Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-tk/tkSimpleDialog.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 102d18ea02cf |
| children | 90c77b8af1a4 |
| 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 import os | |
| 30 | |
| 31 class Dialog(Toplevel): | |
| 32 | |
| 11845 | 33 '''Class to open dialogs. |
| 34 | |
| 35 This class is intended as a base class for custom dialogs | |
| 36 ''' | |
| 37 | |
| 5990 | 38 def __init__(self, parent, title = None): |
| 39 | |
| 11845 | 40 '''Initialize a dialog. |
| 41 | |
| 42 Arguments: | |
| 43 | |
| 44 parent -- a parent window (the application window) | |
| 45 | |
| 46 title -- the dialog title | |
| 47 ''' | |
| 5990 | 48 Toplevel.__init__(self, parent) |
| 49 self.transient(parent) | |
| 50 | |
| 51 if title: | |
| 52 self.title(title) | |
| 53 | |
| 54 self.parent = parent | |
| 55 | |
| 56 self.result = None | |
| 57 | |
| 58 body = Frame(self) | |
| 59 self.initial_focus = self.body(body) | |
| 60 body.pack(padx=5, pady=5) | |
| 61 | |
| 62 self.buttonbox() | |
| 63 | |
| 64 self.grab_set() | |
| 65 | |
| 66 if not self.initial_focus: | |
| 67 self.initial_focus = self | |
| 68 | |
| 69 self.protocol("WM_DELETE_WINDOW", self.cancel) | |
| 70 | |
|
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
|
71 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
|
72 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
|
73 parent.winfo_rooty()+50)) |
| 5990 | 74 |
| 75 self.initial_focus.focus_set() | |
| 76 | |
| 77 self.wait_window(self) | |
| 78 | |
|
11051
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
79 def destroy(self): |
| 11845 | 80 '''Destroy the window''' |
|
11051
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
81 self.initial_focus = None |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
82 Toplevel.destroy(self) |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
83 |
| 5990 | 84 # |
| 85 # construction hooks | |
| 86 | |
| 87 def body(self, master): | |
| 11845 | 88 '''create dialog body. |
| 5990 | 89 |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
90 return widget that should have initial focus. |
| 11845 | 91 This method should be overridden, and is called |
| 92 by the __init__ method. | |
| 93 ''' | |
| 5990 | 94 pass |
| 95 | |
| 96 def buttonbox(self): | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
97 '''add standard button box. |
| 11845 | 98 |
|
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
|
99 override if you do not want the standard buttons |
| 11845 | 100 ''' |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
101 |
| 5990 | 102 box = Frame(self) |
| 103 | |
| 104 w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE) | |
| 105 w.pack(side=LEFT, padx=5, pady=5) | |
| 106 w = Button(box, text="Cancel", width=10, command=self.cancel) | |
| 107 w.pack(side=LEFT, padx=5, pady=5) | |
| 108 | |
| 109 self.bind("<Return>", self.ok) | |
| 110 self.bind("<Escape>", self.cancel) | |
| 111 | |
| 112 box.pack() | |
| 113 | |
| 114 # | |
| 115 # standard button semantics | |
| 116 | |
| 117 def ok(self, event=None): | |
| 118 | |
| 119 if not self.validate(): | |
| 120 self.initial_focus.focus_set() # put focus back | |
| 121 return | |
| 122 | |
| 123 self.withdraw() | |
| 124 self.update_idletasks() | |
| 125 | |
| 126 self.apply() | |
| 127 | |
| 128 self.cancel() | |
| 129 | |
| 130 def cancel(self, event=None): | |
| 131 | |
| 132 # 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
|
133 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
|
134 self.parent.focus_set() |
| 5990 | 135 self.destroy() |
| 136 | |
| 137 # | |
| 138 # command hooks | |
| 139 | |
| 140 def validate(self): | |
| 11845 | 141 '''validate the data |
| 142 | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
143 This method is called automatically to validate the data before the |
| 11845 | 144 dialog is destroyed. By default, it always validates OK. |
| 145 ''' | |
| 5990 | 146 |
| 147 return 1 # override | |
| 148 | |
| 149 def apply(self): | |
| 11845 | 150 '''process the data |
| 151 | |
| 152 This method is called automatically to process the data, *after* | |
| 153 the dialog is destroyed. By default, it does nothing. | |
| 154 ''' | |
| 5990 | 155 |
| 156 pass # override | |
| 157 | |
| 158 | |
| 159 # -------------------------------------------------------------------- | |
| 160 # convenience dialogues | |
| 161 | |
| 162 class _QueryDialog(Dialog): | |
| 163 | |
| 164 def __init__(self, title, prompt, | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
165 initialvalue=None, |
| 5990 | 166 minvalue = None, maxvalue = None, |
| 167 parent = None): | |
| 168 | |
| 169 if not parent: | |
|
9330
fd5a4309bfdc
Only reference Tkinter._default_root when needed.
Guido van Rossum <guido@python.org>
parents:
6013
diff
changeset
|
170 import Tkinter |
|
fd5a4309bfdc
Only reference Tkinter._default_root when needed.
Guido van Rossum <guido@python.org>
parents:
6013
diff
changeset
|
171 parent = Tkinter._default_root |
| 5990 | 172 |
| 173 self.prompt = prompt | |
| 174 self.minvalue = minvalue | |
| 175 self.maxvalue = maxvalue | |
| 176 | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
177 self.initialvalue = initialvalue |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
178 |
| 5990 | 179 Dialog.__init__(self, parent, title) |
| 180 | |
|
11051
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
181 def destroy(self): |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
182 self.entry = None |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
183 Dialog.destroy(self) |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
184 |
| 5990 | 185 def body(self, master): |
| 186 | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
187 w = Label(master, text=self.prompt, justify=LEFT) |
| 5990 | 188 w.grid(row=0, padx=5, sticky=W) |
| 189 | |
| 190 self.entry = Entry(master, name="entry") | |
| 191 self.entry.grid(row=1, padx=5, sticky=W+E) | |
| 192 | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
193 if self.initialvalue: |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
194 self.entry.insert(0, self.initialvalue) |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
195 self.entry.select_range(0, END) |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
196 |
| 5990 | 197 return self.entry |
| 198 | |
| 199 def validate(self): | |
| 200 | |
| 201 import tkMessageBox | |
| 202 | |
| 203 try: | |
| 204 result = self.getresult() | |
| 205 except ValueError: | |
| 206 tkMessageBox.showwarning( | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
207 "Illegal value", |
| 5990 | 208 self.errormessage + "\nPlease try again", |
| 209 parent = self | |
| 210 ) | |
| 211 return 0 | |
| 212 | |
| 213 if self.minvalue is not None and result < self.minvalue: | |
| 214 tkMessageBox.showwarning( | |
| 215 "Too small", | |
| 216 "The allowed minimum value is %s. " | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
217 "Please try again." % self.minvalue, |
| 5990 | 218 parent = self |
| 219 ) | |
| 220 return 0 | |
| 221 | |
| 222 if self.maxvalue is not None and result > self.maxvalue: | |
| 223 tkMessageBox.showwarning( | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
224 "Too large", |
| 5990 | 225 "The allowed maximum value is %s. " |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
226 "Please try again." % self.maxvalue, |
| 5990 | 227 parent = self |
| 228 ) | |
| 229 return 0 | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
230 |
| 5990 | 231 self.result = result |
| 232 | |
| 233 return 1 | |
| 234 | |
| 235 | |
| 236 class _QueryInteger(_QueryDialog): | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
237 errormessage = "Not an integer." |
| 5990 | 238 def getresult(self): |
|
16517
eee67a84e5fa
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15308
diff
changeset
|
239 return int(self.entry.get()) |
| 5990 | 240 |
| 241 def askinteger(title, prompt, **kw): | |
| 11845 | 242 '''get an integer from the user |
| 243 | |
| 244 Arguments: | |
| 245 | |
| 246 title -- the dialog title | |
| 247 prompt -- the label text | |
| 248 **kw -- see SimpleDialog class | |
| 249 | |
| 250 Return value is an integer | |
| 251 ''' | |
| 5990 | 252 d = apply(_QueryInteger, (title, prompt), kw) |
| 253 return d.result | |
| 254 | |
| 255 class _QueryFloat(_QueryDialog): | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
256 errormessage = "Not a floating point value." |
| 5990 | 257 def getresult(self): |
|
16517
eee67a84e5fa
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15308
diff
changeset
|
258 return float(self.entry.get()) |
| 5990 | 259 |
| 260 def askfloat(title, prompt, **kw): | |
| 11845 | 261 '''get a float from the user |
| 262 | |
| 263 Arguments: | |
| 264 | |
| 265 title -- the dialog title | |
| 266 prompt -- the label text | |
| 267 **kw -- see SimpleDialog class | |
| 268 | |
| 269 Return value is a float | |
| 270 ''' | |
| 5990 | 271 d = apply(_QueryFloat, (title, prompt), kw) |
| 272 return d.result | |
| 273 | |
| 274 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
|
275 def __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
|
276 if kw.has_key("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
|
277 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
|
278 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
|
279 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
|
280 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
|
281 _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
|
282 |
|
102d18ea02cf
Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents:
16517
diff
changeset
|
283 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
|
284 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
|
285 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
|
286 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
|
287 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
|
288 |
| 5990 | 289 def getresult(self): |
| 290 return self.entry.get() | |
| 291 | |
| 292 def askstring(title, prompt, **kw): | |
| 11845 | 293 '''get a string from the user |
| 294 | |
| 295 Arguments: | |
| 296 | |
| 297 title -- the dialog title | |
| 298 prompt -- the label text | |
| 299 **kw -- see SimpleDialog class | |
| 300 | |
| 301 Return value is a string | |
| 302 ''' | |
| 5990 | 303 d = apply(_QueryString, (title, prompt), kw) |
| 304 return d.result | |
| 305 | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
306 if __name__ == "__main__": |
| 5990 | 307 |
| 308 root = Tk() | |
| 309 root.update() | |
| 310 | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
311 print askinteger("Spam", "Egg count", initialvalue=12*12) |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
312 print askfloat("Spam", "Egg weight\n(in tons)", minvalue=1, maxvalue=100) |
| 5990 | 313 print askstring("Spam", "Egg label") |
