Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-tk/tkSimpleDialog.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | f98e2944cb40 |
| children |
| rev | line source |
|---|---|
| 5990 | 1 # |
| 2 # An Introduction to Tkinter | |
| 3 # tkSimpleDialog.py | |
| 4 # | |
| 5 # Copyright (c) 1997 by Fredrik Lundh | |
| 6 # | |
| 7 # fredrik@pythonware.com | |
| 8 # http://www.pythonware.com | |
| 9 # | |
| 10 | |
| 11 # -------------------------------------------------------------------- | |
| 12 # dialog base class | |
| 13 | |
| 11845 | 14 '''Dialog boxes |
| 15 | |
| 16 This module handles dialog boxes. It contains the following | |
| 17 public symbols: | |
| 18 | |
| 19 Dialog -- a base class for dialogs | |
| 20 | |
| 21 askinteger -- get an integer from the user | |
| 22 | |
| 23 askfloat -- get a float from the user | |
| 24 | |
| 25 askstring -- get a string from the user | |
| 26 ''' | |
| 27 | |
| 5990 | 28 from Tkinter import * |
| 29 | |
| 30 class Dialog(Toplevel): | |
| 31 | |
| 11845 | 32 '''Class to open dialogs. |
| 33 | |
| 34 This class is intended as a base class for custom dialogs | |
| 35 ''' | |
| 36 | |
| 5990 | 37 def __init__(self, parent, title = None): |
| 38 | |
| 11845 | 39 '''Initialize a dialog. |
| 40 | |
| 41 Arguments: | |
| 42 | |
| 43 parent -- a parent window (the application window) | |
| 44 | |
| 45 title -- the dialog title | |
| 46 ''' | |
| 5990 | 47 Toplevel.__init__(self, parent) |
| 48 | |
|
52059
c79ec63e6c08
Fixed issue #2638: Show a window constructed with tkSimpleDialog.Dialog only
Guilherme Polo <ggpolo@gmail.com>
parents:
47182
diff
changeset
|
49 self.withdraw() # remain invisible for now |
|
40350
89f2e8da0f0f
Patch #1538878: Don't make tkSimpleDialog dialogs transient if
Martin v. Löwis <martin@v.loewis.de>
parents:
32940
diff
changeset
|
50 # If the master is not viewable, don't |
|
89f2e8da0f0f
Patch #1538878: Don't make tkSimpleDialog dialogs transient if
Martin v. Löwis <martin@v.loewis.de>
parents:
32940
diff
changeset
|
51 # make the child transient, or else it |
|
89f2e8da0f0f
Patch #1538878: Don't make tkSimpleDialog dialogs transient if
Martin v. Löwis <martin@v.loewis.de>
parents:
32940
diff
changeset
|
52 # would be opened withdrawn |
|
40732
5dae0d7f683a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
40352
diff
changeset
|
53 if parent.winfo_viewable(): |
|
40350
89f2e8da0f0f
Patch #1538878: Don't make tkSimpleDialog dialogs transient if
Martin v. Löwis <martin@v.loewis.de>
parents:
32940
diff
changeset
|
54 self.transient(parent) |
|
40732
5dae0d7f683a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
40352
diff
changeset
|
55 |
| 5990 | 56 if title: |
| 57 self.title(title) | |
| 58 | |
| 59 self.parent = parent | |
| 60 | |
| 61 self.result = None | |
| 62 | |
| 63 body = Frame(self) | |
| 64 self.initial_focus = self.body(body) | |
| 65 body.pack(padx=5, pady=5) | |
| 66 | |
| 67 self.buttonbox() | |
| 68 | |
| 69 | |
| 70 if not self.initial_focus: | |
| 71 self.initial_focus = self | |
| 72 | |
| 73 self.protocol("WM_DELETE_WINDOW", self.cancel) | |
| 74 | |
|
21259
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
75 if self.parent is not None: |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
76 self.geometry("+%d+%d" % (parent.winfo_rootx()+50, |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
77 parent.winfo_rooty()+50)) |
| 5990 | 78 |
|
52059
c79ec63e6c08
Fixed issue #2638: Show a window constructed with tkSimpleDialog.Dialog only
Guilherme Polo <ggpolo@gmail.com>
parents:
47182
diff
changeset
|
79 self.deiconify() # become visibile now |
|
c79ec63e6c08
Fixed issue #2638: Show a window constructed with tkSimpleDialog.Dialog only
Guilherme Polo <ggpolo@gmail.com>
parents:
47182
diff
changeset
|
80 |
| 5990 | 81 self.initial_focus.focus_set() |
| 82 | |
|
52059
c79ec63e6c08
Fixed issue #2638: Show a window constructed with tkSimpleDialog.Dialog only
Guilherme Polo <ggpolo@gmail.com>
parents:
47182
diff
changeset
|
83 # wait for window to appear on screen before calling grab_set |
|
c79ec63e6c08
Fixed issue #2638: Show a window constructed with tkSimpleDialog.Dialog only
Guilherme Polo <ggpolo@gmail.com>
parents:
47182
diff
changeset
|
84 self.wait_visibility() |
|
c79ec63e6c08
Fixed issue #2638: Show a window constructed with tkSimpleDialog.Dialog only
Guilherme Polo <ggpolo@gmail.com>
parents:
47182
diff
changeset
|
85 self.grab_set() |
| 5990 | 86 self.wait_window(self) |
| 87 | |
|
11051
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
88 def destroy(self): |
| 11845 | 89 '''Destroy the window''' |
|
11051
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
90 self.initial_focus = None |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
91 Toplevel.destroy(self) |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
92 |
| 5990 | 93 # |
| 94 # construction hooks | |
| 95 | |
| 96 def body(self, master): | |
| 11845 | 97 '''create dialog body. |
| 5990 | 98 |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
99 return widget that should have initial focus. |
| 11845 | 100 This method should be overridden, and is called |
| 101 by the __init__ method. | |
| 102 ''' | |
| 5990 | 103 pass |
| 104 | |
| 105 def buttonbox(self): | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
106 '''add standard button box. |
| 11845 | 107 |
|
21259
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
108 override if you do not want the standard buttons |
| 11845 | 109 ''' |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
110 |
| 5990 | 111 box = Frame(self) |
| 112 | |
| 113 w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE) | |
| 114 w.pack(side=LEFT, padx=5, pady=5) | |
| 115 w = Button(box, text="Cancel", width=10, command=self.cancel) | |
| 116 w.pack(side=LEFT, padx=5, pady=5) | |
| 117 | |
| 118 self.bind("<Return>", self.ok) | |
| 119 self.bind("<Escape>", self.cancel) | |
| 120 | |
| 121 box.pack() | |
| 122 | |
| 123 # | |
| 124 # standard button semantics | |
| 125 | |
| 126 def ok(self, event=None): | |
| 127 | |
| 128 if not self.validate(): | |
| 129 self.initial_focus.focus_set() # put focus back | |
| 130 return | |
| 131 | |
| 132 self.withdraw() | |
| 133 self.update_idletasks() | |
| 134 | |
|
40352
5c08dcb7d280
Patch #1594554: Always close a tkSimpleDialog on ok(), even
Martin v. Löwis <martin@v.loewis.de>
parents:
40350
diff
changeset
|
135 try: |
|
5c08dcb7d280
Patch #1594554: Always close a tkSimpleDialog on ok(), even
Martin v. Löwis <martin@v.loewis.de>
parents:
40350
diff
changeset
|
136 self.apply() |
|
5c08dcb7d280
Patch #1594554: Always close a tkSimpleDialog on ok(), even
Martin v. Löwis <martin@v.loewis.de>
parents:
40350
diff
changeset
|
137 finally: |
|
5c08dcb7d280
Patch #1594554: Always close a tkSimpleDialog on ok(), even
Martin v. Löwis <martin@v.loewis.de>
parents:
40350
diff
changeset
|
138 self.cancel() |
| 5990 | 139 |
| 140 def cancel(self, event=None): | |
| 141 | |
| 142 # put focus back to the parent window | |
|
21259
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
143 if self.parent is not None: |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
144 self.parent.focus_set() |
| 5990 | 145 self.destroy() |
| 146 | |
| 147 # | |
| 148 # command hooks | |
| 149 | |
| 150 def validate(self): | |
| 11845 | 151 '''validate the data |
| 152 | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
153 This method is called automatically to validate the data before the |
| 11845 | 154 dialog is destroyed. By default, it always validates OK. |
| 155 ''' | |
| 5990 | 156 |
| 157 return 1 # override | |
| 158 | |
| 159 def apply(self): | |
| 11845 | 160 '''process the data |
| 161 | |
| 162 This method is called automatically to process the data, *after* | |
| 163 the dialog is destroyed. By default, it does nothing. | |
| 164 ''' | |
| 5990 | 165 |
| 166 pass # override | |
| 167 | |
| 168 | |
| 169 # -------------------------------------------------------------------- | |
| 170 # convenience dialogues | |
| 171 | |
| 172 class _QueryDialog(Dialog): | |
| 173 | |
| 174 def __init__(self, title, prompt, | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
175 initialvalue=None, |
| 5990 | 176 minvalue = None, maxvalue = None, |
| 177 parent = None): | |
| 178 | |
| 179 if not parent: | |
|
9330
fd5a4309bfdc
Only reference Tkinter._default_root when needed.
Guido van Rossum <guido@python.org>
parents:
6013
diff
changeset
|
180 import Tkinter |
|
fd5a4309bfdc
Only reference Tkinter._default_root when needed.
Guido van Rossum <guido@python.org>
parents:
6013
diff
changeset
|
181 parent = Tkinter._default_root |
| 5990 | 182 |
| 183 self.prompt = prompt | |
| 184 self.minvalue = minvalue | |
| 185 self.maxvalue = maxvalue | |
| 186 | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
187 self.initialvalue = initialvalue |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
188 |
| 5990 | 189 Dialog.__init__(self, parent, title) |
| 190 | |
|
11051
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
191 def destroy(self): |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
192 self.entry = None |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
193 Dialog.destroy(self) |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
194 |
| 5990 | 195 def body(self, master): |
| 196 | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
197 w = Label(master, text=self.prompt, justify=LEFT) |
| 5990 | 198 w.grid(row=0, padx=5, sticky=W) |
| 199 | |
| 200 self.entry = Entry(master, name="entry") | |
| 201 self.entry.grid(row=1, padx=5, sticky=W+E) | |
| 202 | |
|
78337
f98e2944cb40
Issue #12288: Consider '0' and '0.0' as valid initialvalue for tkinter SimpleDialog.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
68256
diff
changeset
|
203 if self.initialvalue is not None: |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
204 self.entry.insert(0, self.initialvalue) |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
205 self.entry.select_range(0, END) |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
206 |
| 5990 | 207 return self.entry |
| 208 | |
| 209 def validate(self): | |
| 210 | |
| 211 import tkMessageBox | |
| 212 | |
| 213 try: | |
| 214 result = self.getresult() | |
| 215 except ValueError: | |
| 216 tkMessageBox.showwarning( | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
217 "Illegal value", |
| 5990 | 218 self.errormessage + "\nPlease try again", |
| 219 parent = self | |
| 220 ) | |
| 221 return 0 | |
| 222 | |
| 223 if self.minvalue is not None and result < self.minvalue: | |
| 224 tkMessageBox.showwarning( | |
| 225 "Too small", | |
| 226 "The allowed minimum value is %s. " | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
227 "Please try again." % self.minvalue, |
| 5990 | 228 parent = self |
| 229 ) | |
| 230 return 0 | |
| 231 | |
| 232 if self.maxvalue is not None and result > self.maxvalue: | |
| 233 tkMessageBox.showwarning( | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
234 "Too large", |
| 5990 | 235 "The allowed maximum value is %s. " |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
236 "Please try again." % self.maxvalue, |
| 5990 | 237 parent = self |
| 238 ) | |
| 239 return 0 | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
240 |
| 5990 | 241 self.result = result |
| 242 | |
| 243 return 1 | |
| 244 | |
| 245 | |
| 246 class _QueryInteger(_QueryDialog): | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
247 errormessage = "Not an integer." |
| 5990 | 248 def getresult(self): |
|
16517
eee67a84e5fa
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15308
diff
changeset
|
249 return int(self.entry.get()) |
| 5990 | 250 |
| 251 def askinteger(title, prompt, **kw): | |
| 11845 | 252 '''get an integer from the user |
| 253 | |
| 254 Arguments: | |
| 255 | |
| 256 title -- the dialog title | |
| 257 prompt -- the label text | |
| 258 **kw -- see SimpleDialog class | |
| 259 | |
| 260 Return value is an integer | |
| 261 ''' | |
|
28513
90c77b8af1a4
SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents:
21259
diff
changeset
|
262 d = _QueryInteger(title, prompt, **kw) |
| 5990 | 263 return d.result |
| 264 | |
| 265 class _QueryFloat(_QueryDialog): | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
266 errormessage = "Not a floating point value." |
| 5990 | 267 def getresult(self): |
|
16517
eee67a84e5fa
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15308
diff
changeset
|
268 return float(self.entry.get()) |
| 5990 | 269 |
| 270 def askfloat(title, prompt, **kw): | |
| 11845 | 271 '''get a float from the user |
| 272 | |
| 273 Arguments: | |
| 274 | |
| 275 title -- the dialog title | |
| 276 prompt -- the label text | |
| 277 **kw -- see SimpleDialog class | |
| 278 | |
| 279 Return value is a float | |
| 280 ''' | |
|
28513
90c77b8af1a4
SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents:
21259
diff
changeset
|
281 d = _QueryFloat(title, prompt, **kw) |
| 5990 | 282 return d.result |
| 283 | |
| 284 class _QueryString(_QueryDialog): | |
|
21259
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
285 def __init__(self, *args, **kw): |
|
56436
684960fb1cbf
replace has_key with 'in' operator
Benjamin Peterson <benjamin@python.org>
parents:
52059
diff
changeset
|
286 if "show" in kw: |
|
21259
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
287 self.__show = kw["show"] |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
288 del kw["show"] |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
289 else: |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
290 self.__show = None |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
291 _QueryDialog.__init__(self, *args, **kw) |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
292 |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
293 def body(self, master): |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
294 entry = _QueryDialog.body(self, master) |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
295 if self.__show is not None: |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
296 entry.configure(show=self.__show) |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
297 return entry |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
298 |
| 5990 | 299 def getresult(self): |
| 300 return self.entry.get() | |
| 301 | |
| 302 def askstring(title, prompt, **kw): | |
| 11845 | 303 '''get a string from the user |
| 304 | |
| 305 Arguments: | |
| 306 | |
| 307 title -- the dialog title | |
| 308 prompt -- the label text | |
| 309 **kw -- see SimpleDialog class | |
| 310 | |
| 311 Return value is a string | |
| 312 ''' | |
|
28513
90c77b8af1a4
SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents:
21259
diff
changeset
|
313 d = _QueryString(title, prompt, **kw) |
| 5990 | 314 return d.result |
| 315 | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
316 if __name__ == "__main__": |
| 5990 | 317 |
| 318 root = Tk() | |
| 319 root.update() | |
| 320 | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
321 print askinteger("Spam", "Egg count", initialvalue=12*12) |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
322 print askfloat("Spam", "Egg weight\n(in tons)", minvalue=1, maxvalue=100) |
| 5990 | 323 print askstring("Spam", "Egg label") |
