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