| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # An Introduction to Tkinter | 
					
						
							|  |  |  | # tkSimpleDialog.py | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright (c) 1997 by Fredrik Lundh | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # fredrik@pythonware.com | 
					
						
							|  |  |  | # http://www.pythonware.com | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # -------------------------------------------------------------------- | 
					
						
							|  |  |  | # dialog base class | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  | '''Dialog boxes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This module handles dialog boxes. It contains the following | 
					
						
							|  |  |  | public symbols: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Dialog -- a base class for dialogs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | askinteger -- get an integer from the user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | askfloat -- get a float from the user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | askstring -- get a string from the user | 
					
						
							|  |  |  | '''
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  | from Tkinter import * | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Dialog(Toplevel): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |     '''Class to open dialogs.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     This class is intended as a base class for custom dialogs | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |     def __init__(self, parent, title = None): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |         '''Initialize a dialog.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Arguments: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             parent -- a parent window (the application window) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             title -- the dialog title | 
					
						
							|  |  |  |         '''
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |         Toplevel.__init__(self, parent) | 
					
						
							|  |  |  |         self.transient(parent) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if title: | 
					
						
							|  |  |  |             self.title(title) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.parent = parent | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.result = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         body = Frame(self) | 
					
						
							|  |  |  |         self.initial_focus = self.body(body) | 
					
						
							|  |  |  |         body.pack(padx=5, pady=5) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.buttonbox() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-03 18:36:25 +00:00
										 |  |  |         self.wait_visibility() # window needs to be visible for the grab | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |         self.grab_set() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not self.initial_focus: | 
					
						
							|  |  |  |             self.initial_focus = self | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.protocol("WM_DELETE_WINDOW", self.cancel) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-06 16:51:41 +00:00
										 |  |  |         if self.parent is not None: | 
					
						
							|  |  |  |             self.geometry("+%d+%d" % (parent.winfo_rootx()+50, | 
					
						
							|  |  |  |                                       parent.winfo_rooty()+50)) | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.initial_focus.focus_set() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.wait_window(self) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-06-25 15:53:54 +00:00
										 |  |  |     def destroy(self): | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |         '''Destroy the window''' | 
					
						
							| 
									
										
										
										
											1999-06-25 15:53:54 +00:00
										 |  |  |         self.initial_focus = None | 
					
						
							|  |  |  |         Toplevel.destroy(self) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |     # | 
					
						
							|  |  |  |     # construction hooks | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def body(self, master): | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |         '''create dialog body.
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-23 18:31:14 +00:00
										 |  |  |         return widget that should have initial focus. | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |         This method should be overridden, and is called | 
					
						
							|  |  |  |         by the __init__ method. | 
					
						
							|  |  |  |         '''
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def buttonbox(self): | 
					
						
							| 
									
										
										
										
											2000-10-23 18:31:14 +00:00
										 |  |  |         '''add standard button box.
 | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-06 16:51:41 +00:00
										 |  |  |         override if you do not want the standard buttons | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |         '''
 | 
					
						
							| 
									
										
										
										
											2000-10-23 18:31:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |         box = Frame(self) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE) | 
					
						
							|  |  |  |         w.pack(side=LEFT, padx=5, pady=5) | 
					
						
							|  |  |  |         w = Button(box, text="Cancel", width=10, command=self.cancel) | 
					
						
							|  |  |  |         w.pack(side=LEFT, padx=5, pady=5) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.bind("<Return>", self.ok) | 
					
						
							|  |  |  |         self.bind("<Escape>", self.cancel) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         box.pack() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # standard button semantics | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def ok(self, event=None): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not self.validate(): | 
					
						
							|  |  |  |             self.initial_focus.focus_set() # put focus back | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.withdraw() | 
					
						
							|  |  |  |         self.update_idletasks() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.apply() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def cancel(self, event=None): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # put focus back to the parent window | 
					
						
							| 
									
										
										
										
											2001-12-06 16:51:41 +00:00
										 |  |  |         if self.parent is not None: | 
					
						
							|  |  |  |             self.parent.focus_set() | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |         self.destroy() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # command hooks | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def validate(self): | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |         '''validate the data
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-23 18:31:14 +00:00
										 |  |  |         This method is called automatically to validate the data before the | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |         dialog is destroyed. By default, it always validates OK. | 
					
						
							|  |  |  |         '''
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return 1 # override | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def apply(self): | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |         '''process the data
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         This method is called automatically to process the data, *after* | 
					
						
							|  |  |  |         the dialog is destroyed. By default, it does nothing. | 
					
						
							|  |  |  |         '''
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         pass # override | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # -------------------------------------------------------------------- | 
					
						
							|  |  |  | # convenience dialogues | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _QueryDialog(Dialog): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, title, prompt, | 
					
						
							| 
									
										
										
										
											1997-08-14 14:17:28 +00:00
										 |  |  |                  initialvalue=None, | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |                  minvalue = None, maxvalue = None, | 
					
						
							|  |  |  |                  parent = None): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not parent: | 
					
						
							| 
									
										
										
										
											1998-10-12 20:40:09 +00:00
										 |  |  |             import Tkinter | 
					
						
							|  |  |  |             parent = Tkinter._default_root | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.prompt   = prompt | 
					
						
							|  |  |  |         self.minvalue = minvalue | 
					
						
							|  |  |  |         self.maxvalue = maxvalue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-14 14:17:28 +00:00
										 |  |  |         self.initialvalue = initialvalue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |         Dialog.__init__(self, parent, title) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-06-25 15:53:54 +00:00
										 |  |  |     def destroy(self): | 
					
						
							|  |  |  |         self.entry = None | 
					
						
							|  |  |  |         Dialog.destroy(self) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |     def body(self, master): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-14 14:17:28 +00:00
										 |  |  |         w = Label(master, text=self.prompt, justify=LEFT) | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |         w.grid(row=0, padx=5, sticky=W) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.entry = Entry(master, name="entry") | 
					
						
							|  |  |  |         self.entry.grid(row=1, padx=5, sticky=W+E) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-14 14:17:28 +00:00
										 |  |  |         if self.initialvalue: | 
					
						
							|  |  |  |             self.entry.insert(0, self.initialvalue) | 
					
						
							|  |  |  |             self.entry.select_range(0, END) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |         return self.entry | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def validate(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         import tkMessageBox | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result = self.getresult() | 
					
						
							|  |  |  |         except ValueError: | 
					
						
							|  |  |  |             tkMessageBox.showwarning( | 
					
						
							| 
									
										
										
										
											1997-08-14 14:17:28 +00:00
										 |  |  |                 "Illegal value", | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |                 self.errormessage + "\nPlease try again", | 
					
						
							|  |  |  |                 parent = self | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             return 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if self.minvalue is not None and result < self.minvalue: | 
					
						
							|  |  |  |             tkMessageBox.showwarning( | 
					
						
							|  |  |  |                 "Too small", | 
					
						
							|  |  |  |                 "The allowed minimum value is %s. " | 
					
						
							| 
									
										
										
										
											1997-08-14 14:17:28 +00:00
										 |  |  |                 "Please try again." % self.minvalue, | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |                 parent = self | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             return 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if self.maxvalue is not None and result > self.maxvalue: | 
					
						
							|  |  |  |             tkMessageBox.showwarning( | 
					
						
							| 
									
										
										
										
											1997-08-14 14:17:28 +00:00
										 |  |  |                 "Too large", | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |                 "The allowed maximum value is %s. " | 
					
						
							| 
									
										
										
										
											1997-08-14 14:17:28 +00:00
										 |  |  |                 "Please try again." % self.maxvalue, | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |                 parent = self | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             return 0 | 
					
						
							| 
									
										
										
										
											2000-10-23 18:31:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |         self.result = result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _QueryInteger(_QueryDialog): | 
					
						
							| 
									
										
										
										
											1997-08-14 14:17:28 +00:00
										 |  |  |     errormessage = "Not an integer." | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |     def getresult(self): | 
					
						
							| 
									
										
										
										
											2001-02-09 11:51:27 +00:00
										 |  |  |         return int(self.entry.get()) | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def askinteger(title, prompt, **kw): | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |     '''get an integer from the user
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Arguments: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         title -- the dialog title | 
					
						
							|  |  |  |         prompt -- the label text | 
					
						
							|  |  |  |         **kw -- see SimpleDialog class | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Return value is an integer | 
					
						
							|  |  |  |     '''
 | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  |     d = _QueryInteger(title, prompt, **kw) | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |     return d.result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _QueryFloat(_QueryDialog): | 
					
						
							| 
									
										
										
										
											1997-08-14 14:17:28 +00:00
										 |  |  |     errormessage = "Not a floating point value." | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |     def getresult(self): | 
					
						
							| 
									
										
										
										
											2001-02-09 11:51:27 +00:00
										 |  |  |         return float(self.entry.get()) | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def askfloat(title, prompt, **kw): | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |     '''get a float from the user
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Arguments: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         title -- the dialog title | 
					
						
							|  |  |  |         prompt -- the label text | 
					
						
							|  |  |  |         **kw -- see SimpleDialog class | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Return value is a float | 
					
						
							|  |  |  |     '''
 | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  |     d = _QueryFloat(title, prompt, **kw) | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |     return d.result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _QueryString(_QueryDialog): | 
					
						
							| 
									
										
										
										
											2001-12-06 16:51:41 +00:00
										 |  |  |     def __init__(self, *args, **kw): | 
					
						
							|  |  |  |         if kw.has_key("show"): | 
					
						
							|  |  |  |             self.__show = kw["show"] | 
					
						
							|  |  |  |             del kw["show"] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.__show = None | 
					
						
							|  |  |  |         _QueryDialog.__init__(self, *args, **kw) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def body(self, master): | 
					
						
							|  |  |  |         entry = _QueryDialog.body(self, master) | 
					
						
							|  |  |  |         if self.__show is not None: | 
					
						
							|  |  |  |             entry.configure(show=self.__show) | 
					
						
							|  |  |  |         return entry | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |     def getresult(self): | 
					
						
							|  |  |  |         return self.entry.get() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def askstring(title, prompt, **kw): | 
					
						
							| 
									
										
										
										
											2000-02-24 15:01:43 +00:00
										 |  |  |     '''get a string from the user
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Arguments: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         title -- the dialog title | 
					
						
							|  |  |  |         prompt -- the label text | 
					
						
							|  |  |  |         **kw -- see SimpleDialog class | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Return value is a string | 
					
						
							|  |  |  |     '''
 | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  |     d = _QueryString(title, prompt, **kw) | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |     return d.result | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-23 18:31:14 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     root = Tk() | 
					
						
							|  |  |  |     root.update() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-14 14:17:28 +00:00
										 |  |  |     print askinteger("Spam", "Egg count", initialvalue=12*12) | 
					
						
							|  |  |  |     print askfloat("Spam", "Egg weight\n(in tons)", minvalue=1, maxvalue=100) | 
					
						
							| 
									
										
										
										
											1997-08-12 18:21:21 +00:00
										 |  |  |     print askstring("Spam", "Egg label") |