annotate Lib/tkinter/simpledialog.py @ 70817:dd775b1561ba

Fix markup.
author Georg Brandl <georg@python.org>
date Tue, 14 Jun 2011 21:09:55 +0200
parents 6dbb2c1c0900
children fd5166fdb978
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
1 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
2 # An Introduction to Tkinter
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
3 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
4 # Copyright (c) 1997 by Fredrik Lundh
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
5 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
6 # This copyright applies to Dialog, askinteger, askfloat and asktring
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
7 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
8 # fredrik@pythonware.com
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
9 # http://www.pythonware.com
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
10 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
11 """This modules handles dialog boxes.
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
13 It contains the following public symbols:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
14
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
15 SimpleDialog -- A simple but flexible modal dialog box
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
16
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
17 Dialog -- a base class for dialogs
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
18
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
19 askinteger -- get an integer from the user
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
20
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
21 askfloat -- get a float from the user
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
22
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
23 askstring -- get a string from the user
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
24 """
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
47130
ef4cca2d6171 Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents: 47129
diff changeset
26 from tkinter import *
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
27 from tkinter import messagebox
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
29 import tkinter # used at _QueryDialog for tkinter._default_root
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 class SimpleDialog:
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 def __init__(self, master,
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
34 text='', buttons=[], default=None, cancel=None,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
35 title=None, class_=None):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
36 if class_:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
37 self.root = Toplevel(master, class_=class_)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
38 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
39 self.root = Toplevel(master)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
40 if title:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
41 self.root.title(title)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
42 self.root.iconname(title)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
43 self.message = Message(self.root, text=text, aspect=400)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
44 self.message.pack(expand=1, fill=BOTH)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
45 self.frame = Frame(self.root)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
46 self.frame.pack()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
47 self.num = default
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
48 self.cancel = cancel
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
49 self.default = default
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
50 self.root.bind('<Return>', self.return_event)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
51 for num in range(len(buttons)):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
52 s = buttons[num]
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
53 b = Button(self.frame, text=s,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
54 command=(lambda self=self, num=num: self.done(num)))
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
55 if num == default:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
56 b.config(relief=RIDGE, borderwidth=8)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
57 b.pack(side=LEFT, fill=BOTH, expand=1)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
58 self.root.protocol('WM_DELETE_WINDOW', self.wm_delete_window)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
59 self._set_transient(master)
3757
15b1b03ade93 Don't use tktools, dummy! Reworded test message slightly.
Guido van Rossum <guido@python.org>
parents: 3688
diff changeset
60
15b1b03ade93 Don't use tktools, dummy! Reworded test message slightly.
Guido van Rossum <guido@python.org>
parents: 3688
diff changeset
61 def _set_transient(self, master, relx=0.5, rely=0.3):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
62 widget = self.root
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
63 widget.withdraw() # Remain invisible while we figure out the geometry
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
64 widget.transient(master)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
65 widget.update_idletasks() # Actualize geometry information
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
66 if master.winfo_ismapped():
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
67 m_width = master.winfo_width()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
68 m_height = master.winfo_height()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
69 m_x = master.winfo_rootx()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
70 m_y = master.winfo_rooty()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
71 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
72 m_width = master.winfo_screenwidth()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
73 m_height = master.winfo_screenheight()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
74 m_x = m_y = 0
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
75 w_width = widget.winfo_reqwidth()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
76 w_height = widget.winfo_reqheight()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
77 x = m_x + (m_width - w_width) * relx
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
78 y = m_y + (m_height - w_height) * rely
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
79 if x+w_width > master.winfo_screenwidth():
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
80 x = master.winfo_screenwidth() - w_width
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
81 elif x < 0:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
82 x = 0
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
83 if y+w_height > master.winfo_screenheight():
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
84 y = master.winfo_screenheight() - w_height
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
85 elif y < 0:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
86 y = 0
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
87 widget.geometry("+%d+%d" % (x, y))
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
88 widget.deiconify() # Become visible at the desired location
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 def go(self):
32940
22ded309648b Wait for visibility before setting a grab. Fixes #946153.
Martin v. Löwis <martin@v.loewis.de>
parents: 31776
diff changeset
91 self.root.wait_visibility()
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
92 self.root.grab_set()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
93 self.root.mainloop()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
94 self.root.destroy()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
95 return self.num
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 def return_event(self, event):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
98 if self.default is None:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
99 self.root.bell()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
100 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
101 self.done(self.default)
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 def wm_delete_window(self):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
104 if self.cancel is None:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
105 self.root.bell()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
106 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
107 self.done(self.cancel)
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 def done(self, num):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
110 self.num = num
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
111 self.root.quit()
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
114 class Dialog(Toplevel):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
115
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
116 '''Class to open dialogs.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
117
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
118 This class is intended as a base class for custom dialogs
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
119 '''
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
120
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
121 def __init__(self, parent, title = None):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
122
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
123 '''Initialize a dialog.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
124
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
125 Arguments:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
126
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
127 parent -- a parent window (the application window)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
128
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
129 title -- the dialog title
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
130 '''
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
131 Toplevel.__init__(self, parent)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
132
52061
6dbb2c1c0900 Merged revisions 70223 via svnmerge from
Guilherme Polo <ggpolo@gmail.com>
parents: 47132
diff changeset
133 self.withdraw() # remain invisible for now
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
134 # If the master is not viewable, don't
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
135 # make the child transient, or else it
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
136 # would be opened withdrawn
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
137 if parent.winfo_viewable():
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
138 self.transient(parent)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
139
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
140 if title:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
141 self.title(title)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
142
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
143 self.parent = parent
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
144
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
145 self.result = None
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
146
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
147 body = Frame(self)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
148 self.initial_focus = self.body(body)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
149 body.pack(padx=5, pady=5)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
150
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
151 self.buttonbox()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
152
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
153 if not self.initial_focus:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
154 self.initial_focus = self
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
155
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
156 self.protocol("WM_DELETE_WINDOW", self.cancel)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
157
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
158 if self.parent is not None:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
159 self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
160 parent.winfo_rooty()+50))
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
161
52061
6dbb2c1c0900 Merged revisions 70223 via svnmerge from
Guilherme Polo <ggpolo@gmail.com>
parents: 47132
diff changeset
162 self.deiconify() # become visible now
6dbb2c1c0900 Merged revisions 70223 via svnmerge from
Guilherme Polo <ggpolo@gmail.com>
parents: 47132
diff changeset
163
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
164 self.initial_focus.focus_set()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
165
52061
6dbb2c1c0900 Merged revisions 70223 via svnmerge from
Guilherme Polo <ggpolo@gmail.com>
parents: 47132
diff changeset
166 # wait for window to appear on screen before calling grab_set
6dbb2c1c0900 Merged revisions 70223 via svnmerge from
Guilherme Polo <ggpolo@gmail.com>
parents: 47132
diff changeset
167 self.wait_visibility()
6dbb2c1c0900 Merged revisions 70223 via svnmerge from
Guilherme Polo <ggpolo@gmail.com>
parents: 47132
diff changeset
168 self.grab_set()
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
169 self.wait_window(self)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
170
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
171 def destroy(self):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
172 '''Destroy the window'''
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
173 self.initial_focus = None
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
174 Toplevel.destroy(self)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
175
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
176 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
177 # construction hooks
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
178
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
179 def body(self, master):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
180 '''create dialog body.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
181
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
182 return widget that should have initial focus.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
183 This method should be overridden, and is called
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
184 by the __init__ method.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
185 '''
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
186 pass
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
187
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
188 def buttonbox(self):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
189 '''add standard button box.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
190
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
191 override if you do not want the standard buttons
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
192 '''
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
193
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
194 box = Frame(self)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
195
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
196 w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
197 w.pack(side=LEFT, padx=5, pady=5)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
198 w = Button(box, text="Cancel", width=10, command=self.cancel)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
199 w.pack(side=LEFT, padx=5, pady=5)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
200
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
201 self.bind("<Return>", self.ok)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
202 self.bind("<Escape>", self.cancel)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
203
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
204 box.pack()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
205
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
206 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
207 # standard button semantics
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
208
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
209 def ok(self, event=None):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
210
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
211 if not self.validate():
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
212 self.initial_focus.focus_set() # put focus back
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
213 return
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
214
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
215 self.withdraw()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
216 self.update_idletasks()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
217
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
218 try:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
219 self.apply()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
220 finally:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
221 self.cancel()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
222
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
223 def cancel(self, event=None):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
224
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
225 # put focus back to the parent window
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
226 if self.parent is not None:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
227 self.parent.focus_set()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
228 self.destroy()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
229
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
230 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
231 # command hooks
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
232
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
233 def validate(self):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
234 '''validate the data
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
235
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
236 This method is called automatically to validate the data before the
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
237 dialog is destroyed. By default, it always validates OK.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
238 '''
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
239
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
240 return 1 # override
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
241
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
242 def apply(self):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
243 '''process the data
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
244
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
245 This method is called automatically to process the data, *after*
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
246 the dialog is destroyed. By default, it does nothing.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
247 '''
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
248
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
249 pass # override
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
250
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
251
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
252 # --------------------------------------------------------------------
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
253 # convenience dialogues
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
254
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
255 class _QueryDialog(Dialog):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
256
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
257 def __init__(self, title, prompt,
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
258 initialvalue=None,
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
259 minvalue = None, maxvalue = None,
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
260 parent = None):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
261
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
262 if not parent:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
263 parent = tkinter._default_root
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
264
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
265 self.prompt = prompt
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
266 self.minvalue = minvalue
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
267 self.maxvalue = maxvalue
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
268
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
269 self.initialvalue = initialvalue
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
270
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
271 Dialog.__init__(self, parent, title)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
272
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
273 def destroy(self):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
274 self.entry = None
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
275 Dialog.destroy(self)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
276
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
277 def body(self, master):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
278
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
279 w = Label(master, text=self.prompt, justify=LEFT)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
280 w.grid(row=0, padx=5, sticky=W)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
281
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
282 self.entry = Entry(master, name="entry")
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
283 self.entry.grid(row=1, padx=5, sticky=W+E)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
284
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
285 if self.initialvalue:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
286 self.entry.insert(0, self.initialvalue)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
287 self.entry.select_range(0, END)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
288
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
289 return self.entry
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
290
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
291 def validate(self):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
292 try:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
293 result = self.getresult()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
294 except ValueError:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
295 messagebox.showwarning(
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
296 "Illegal value",
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
297 self.errormessage + "\nPlease try again",
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
298 parent = self
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
299 )
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
300 return 0
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
301
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
302 if self.minvalue is not None and result < self.minvalue:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
303 messagebox.showwarning(
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
304 "Too small",
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
305 "The allowed minimum value is %s. "
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
306 "Please try again." % self.minvalue,
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
307 parent = self
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
308 )
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
309 return 0
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
310
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
311 if self.maxvalue is not None and result > self.maxvalue:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
312 messagebox.showwarning(
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
313 "Too large",
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
314 "The allowed maximum value is %s. "
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
315 "Please try again." % self.maxvalue,
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
316 parent = self
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
317 )
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
318 return 0
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
319
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
320 self.result = result
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
321
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
322 return 1
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
323
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
324
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
325 class _QueryInteger(_QueryDialog):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
326 errormessage = "Not an integer."
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
327 def getresult(self):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
328 return int(self.entry.get())
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
329
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
330 def askinteger(title, prompt, **kw):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
331 '''get an integer from the user
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
332
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
333 Arguments:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
334
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
335 title -- the dialog title
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
336 prompt -- the label text
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
337 **kw -- see SimpleDialog class
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
338
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
339 Return value is an integer
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
340 '''
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
341 d = _QueryInteger(title, prompt, **kw)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
342 return d.result
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
343
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
344 class _QueryFloat(_QueryDialog):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
345 errormessage = "Not a floating point value."
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
346 def getresult(self):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
347 return float(self.entry.get())
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
348
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
349 def askfloat(title, prompt, **kw):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
350 '''get a float from the user
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
351
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
352 Arguments:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
353
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
354 title -- the dialog title
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
355 prompt -- the label text
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
356 **kw -- see SimpleDialog class
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
357
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
358 Return value is a float
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
359 '''
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
360 d = _QueryFloat(title, prompt, **kw)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
361 return d.result
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
362
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
363 class _QueryString(_QueryDialog):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
364 def __init__(self, *args, **kw):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
365 if "show" in kw:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
366 self.__show = kw["show"]
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
367 del kw["show"]
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
368 else:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
369 self.__show = None
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
370 _QueryDialog.__init__(self, *args, **kw)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
371
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
372 def body(self, master):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
373 entry = _QueryDialog.body(self, master)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
374 if self.__show is not None:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
375 entry.configure(show=self.__show)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
376 return entry
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
377
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
378 def getresult(self):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
379 return self.entry.get()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
380
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
381 def askstring(title, prompt, **kw):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
382 '''get a string from the user
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
383
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
384 Arguments:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
385
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
386 title -- the dialog title
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
387 prompt -- the label text
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
388 **kw -- see SimpleDialog class
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
389
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
390 Return value is a string
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
391 '''
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
392 d = _QueryString(title, prompt, **kw)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
393 return d.result
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
394
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
395
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
396
31776
29be729b32c3 Patch #911176: Move test function into __main__
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
397 if __name__ == '__main__':
29be729b32c3 Patch #911176: Move test function into __main__
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
398
29be729b32c3 Patch #911176: Move test function into __main__
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
399 def test():
29be729b32c3 Patch #911176: Move test function into __main__
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
400 root = Tk()
29be729b32c3 Patch #911176: Move test function into __main__
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
401 def doit(root=root):
29be729b32c3 Patch #911176: Move test function into __main__
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
402 d = SimpleDialog(root,
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
403 text="This is a test dialog. "
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
404 "Would this have been an actual dialog, "
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
405 "the buttons below would have been glowing "
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
406 "in soft pink light.\n"
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
407 "Do you believe this?",
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
408 buttons=["Yes", "No", "Cancel"],
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
409 default=0,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
410 cancel=2,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
411 title="Test Dialog")
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 32940
diff changeset
412 print(d.go())
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
413 print(askinteger("Spam", "Egg count", initialvalue=12*12))
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
414 print(askfloat("Spam", "Egg weight\n(in tons)", minvalue=1,
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
415 maxvalue=100))
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
416 print(askstring("Spam", "Egg label"))
31776
29be729b32c3 Patch #911176: Move test function into __main__
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
417 t = Button(root, text='Test', command=doit)
29be729b32c3 Patch #911176: Move test function into __main__
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
418 t.pack()
29be729b32c3 Patch #911176: Move test function into __main__
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
419 q = Button(root, text='Quit', command=t.quit)
29be729b32c3 Patch #911176: Move test function into __main__
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
420 q.pack()
29be729b32c3 Patch #911176: Move test function into __main__
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
421 t.mainloop()
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
422
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 test()