Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-tk/tkSimpleDialog.py @ 17322:bd110eadc19a legacy-trunk
One-liner critical fix from Jim Fulton: Eric's string-method crusade
got the order backwards in a line (for .find()).
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Thu, 05 Apr 2001 22:26:23 +0000 |
| parents | eee67a84e5fa |
| children | 102d18ea02cf |
| 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 | |
| 71 self.geometry("+%d+%d" % (parent.winfo_rootx()+50, | |
| 72 parent.winfo_rooty()+50)) | |
| 73 | |
| 74 self.initial_focus.focus_set() | |
| 75 | |
| 76 self.wait_window(self) | |
| 77 | |
|
11051
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
78 def destroy(self): |
| 11845 | 79 '''Destroy the window''' |
|
11051
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
80 self.initial_focus = None |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
81 Toplevel.destroy(self) |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
82 |
| 5990 | 83 # |
| 84 # construction hooks | |
| 85 | |
| 86 def body(self, master): | |
| 11845 | 87 '''create dialog body. |
| 5990 | 88 |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
89 return widget that should have initial focus. |
| 11845 | 90 This method should be overridden, and is called |
| 91 by the __init__ method. | |
| 92 ''' | |
| 5990 | 93 pass |
| 94 | |
| 95 def buttonbox(self): | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
96 '''add standard button box. |
| 11845 | 97 |
| 98 override if you don't want the standard buttons | |
| 99 ''' | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
100 |
| 5990 | 101 box = Frame(self) |
| 102 | |
| 103 w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE) | |
| 104 w.pack(side=LEFT, padx=5, pady=5) | |
| 105 w = Button(box, text="Cancel", width=10, command=self.cancel) | |
| 106 w.pack(side=LEFT, padx=5, pady=5) | |
| 107 | |
| 108 self.bind("<Return>", self.ok) | |
| 109 self.bind("<Escape>", self.cancel) | |
| 110 | |
| 111 box.pack() | |
| 112 | |
| 113 # | |
| 114 # standard button semantics | |
| 115 | |
| 116 def ok(self, event=None): | |
| 117 | |
| 118 if not self.validate(): | |
| 119 self.initial_focus.focus_set() # put focus back | |
| 120 return | |
| 121 | |
| 122 self.withdraw() | |
| 123 self.update_idletasks() | |
| 124 | |
| 125 self.apply() | |
| 126 | |
| 127 self.cancel() | |
| 128 | |
| 129 def cancel(self, event=None): | |
| 130 | |
| 131 # put focus back to the parent window | |
| 132 self.parent.focus_set() | |
| 133 self.destroy() | |
| 134 | |
| 135 # | |
| 136 # command hooks | |
| 137 | |
| 138 def validate(self): | |
| 11845 | 139 '''validate the data |
| 140 | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
141 This method is called automatically to validate the data before the |
| 11845 | 142 dialog is destroyed. By default, it always validates OK. |
| 143 ''' | |
| 5990 | 144 |
| 145 return 1 # override | |
| 146 | |
| 147 def apply(self): | |
| 11845 | 148 '''process the data |
| 149 | |
| 150 This method is called automatically to process the data, *after* | |
| 151 the dialog is destroyed. By default, it does nothing. | |
| 152 ''' | |
| 5990 | 153 |
| 154 pass # override | |
| 155 | |
| 156 | |
| 157 # -------------------------------------------------------------------- | |
| 158 # convenience dialogues | |
| 159 | |
| 160 class _QueryDialog(Dialog): | |
| 161 | |
| 162 def __init__(self, title, prompt, | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
163 initialvalue=None, |
| 5990 | 164 minvalue = None, maxvalue = None, |
| 165 parent = None): | |
| 166 | |
| 167 if not parent: | |
|
9330
fd5a4309bfdc
Only reference Tkinter._default_root when needed.
Guido van Rossum <guido@python.org>
parents:
6013
diff
changeset
|
168 import Tkinter |
|
fd5a4309bfdc
Only reference Tkinter._default_root when needed.
Guido van Rossum <guido@python.org>
parents:
6013
diff
changeset
|
169 parent = Tkinter._default_root |
| 5990 | 170 |
| 171 self.prompt = prompt | |
| 172 self.minvalue = minvalue | |
| 173 self.maxvalue = maxvalue | |
| 174 | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
175 self.initialvalue = initialvalue |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
176 |
| 5990 | 177 Dialog.__init__(self, parent, title) |
| 178 | |
|
11051
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
179 def destroy(self): |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
180 self.entry = None |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
181 Dialog.destroy(self) |
|
27fbb08f8c38
Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents:
9330
diff
changeset
|
182 |
| 5990 | 183 def body(self, master): |
| 184 | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
185 w = Label(master, text=self.prompt, justify=LEFT) |
| 5990 | 186 w.grid(row=0, padx=5, sticky=W) |
| 187 | |
| 188 self.entry = Entry(master, name="entry") | |
| 189 self.entry.grid(row=1, padx=5, sticky=W+E) | |
| 190 | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
191 if self.initialvalue: |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
192 self.entry.insert(0, self.initialvalue) |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
193 self.entry.select_range(0, END) |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
194 |
| 5990 | 195 return self.entry |
| 196 | |
| 197 def validate(self): | |
| 198 | |
| 199 import tkMessageBox | |
| 200 | |
| 201 try: | |
| 202 result = self.getresult() | |
| 203 except ValueError: | |
| 204 tkMessageBox.showwarning( | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
205 "Illegal value", |
| 5990 | 206 self.errormessage + "\nPlease try again", |
| 207 parent = self | |
| 208 ) | |
| 209 return 0 | |
| 210 | |
| 211 if self.minvalue is not None and result < self.minvalue: | |
| 212 tkMessageBox.showwarning( | |
| 213 "Too small", | |
| 214 "The allowed minimum value is %s. " | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
215 "Please try again." % self.minvalue, |
| 5990 | 216 parent = self |
| 217 ) | |
| 218 return 0 | |
| 219 | |
| 220 if self.maxvalue is not None and result > self.maxvalue: | |
| 221 tkMessageBox.showwarning( | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
222 "Too large", |
| 5990 | 223 "The allowed maximum value is %s. " |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
224 "Please try again." % self.maxvalue, |
| 5990 | 225 parent = self |
| 226 ) | |
| 227 return 0 | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
228 |
| 5990 | 229 self.result = result |
| 230 | |
| 231 return 1 | |
| 232 | |
| 233 | |
| 234 class _QueryInteger(_QueryDialog): | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
235 errormessage = "Not an integer." |
| 5990 | 236 def getresult(self): |
|
16517
eee67a84e5fa
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15308
diff
changeset
|
237 return int(self.entry.get()) |
| 5990 | 238 |
| 239 def askinteger(title, prompt, **kw): | |
| 11845 | 240 '''get an integer from the user |
| 241 | |
| 242 Arguments: | |
| 243 | |
| 244 title -- the dialog title | |
| 245 prompt -- the label text | |
| 246 **kw -- see SimpleDialog class | |
| 247 | |
| 248 Return value is an integer | |
| 249 ''' | |
| 5990 | 250 d = apply(_QueryInteger, (title, prompt), kw) |
| 251 return d.result | |
| 252 | |
| 253 class _QueryFloat(_QueryDialog): | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
254 errormessage = "Not a floating point value." |
| 5990 | 255 def getresult(self): |
|
16517
eee67a84e5fa
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15308
diff
changeset
|
256 return float(self.entry.get()) |
| 5990 | 257 |
| 258 def askfloat(title, prompt, **kw): | |
| 11845 | 259 '''get a float from the user |
| 260 | |
| 261 Arguments: | |
| 262 | |
| 263 title -- the dialog title | |
| 264 prompt -- the label text | |
| 265 **kw -- see SimpleDialog class | |
| 266 | |
| 267 Return value is a float | |
| 268 ''' | |
| 5990 | 269 d = apply(_QueryFloat, (title, prompt), kw) |
| 270 return d.result | |
| 271 | |
| 272 class _QueryString(_QueryDialog): | |
| 273 def getresult(self): | |
| 274 return self.entry.get() | |
| 275 | |
| 276 def askstring(title, prompt, **kw): | |
| 11845 | 277 '''get a string from the user |
| 278 | |
| 279 Arguments: | |
| 280 | |
| 281 title -- the dialog title | |
| 282 prompt -- the label text | |
| 283 **kw -- see SimpleDialog class | |
| 284 | |
| 285 Return value is a string | |
| 286 ''' | |
| 5990 | 287 d = apply(_QueryString, (title, prompt), kw) |
| 288 return d.result | |
| 289 | |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
11845
diff
changeset
|
290 if __name__ == "__main__": |
| 5990 | 291 |
| 292 root = Tk() | |
| 293 root.update() | |
| 294 | |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
295 print askinteger("Spam", "Egg count", initialvalue=12*12) |
|
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5990
diff
changeset
|
296 print askfloat("Spam", "Egg weight\n(in tons)", minvalue=1, maxvalue=100) |
| 5990 | 297 print askstring("Spam", "Egg label") |
