2 # An Introduction to Tkinter
5 # Copyright (c) 1997 by Fredrik Lundh
7 # fredrik@pythonware.com
8 # http://www.pythonware.com
11 # --------------------------------------------------------------------
16 This module handles dialog boxes. It contains the following
19 Dialog -- a base class for dialogs
21 askinteger -- get an integer from the user
23 askfloat -- get a float from the user
25 askstring -- get a string from the user
31 class Dialog(Toplevel
):
33 '''Class to open dialogs.
35 This class is intended as a base class for custom dialogs
38 def __init__(self
, parent
, title
= None):
40 '''Initialize a dialog.
44 parent -- a parent window (the application window)
46 title -- the dialog title
48 Toplevel
.__init
__(self
, parent
)
49 self
.transient(parent
)
59 self
.initial_focus
= self
.body(body
)
60 body
.pack(padx
=5, pady
=5)
66 if not self
.initial_focus
:
67 self
.initial_focus
= self
69 self
.protocol("WM_DELETE_WINDOW", self
.cancel
)
71 if self
.parent
is not None:
72 self
.geometry("+%d+%d" % (parent
.winfo_rootx()+50,
73 parent
.winfo_rooty()+50))
75 self
.initial_focus
.focus_set()
77 self
.wait_window(self
)
80 '''Destroy the window'''
81 self
.initial_focus
= None
82 Toplevel
.destroy(self
)
87 def body(self
, master
):
88 '''create dialog body.
90 return widget that should have initial focus.
91 This method should be overridden, and is called
92 by the __init__ method.
97 '''add standard button box.
99 override if you do not want the standard buttons
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)
109 self
.bind("<Return>", self
.ok
)
110 self
.bind("<Escape>", self
.cancel
)
115 # standard button semantics
117 def ok(self
, event
=None):
119 if not self
.validate():
120 self
.initial_focus
.focus_set() # put focus back
124 self
.update_idletasks()
130 def cancel(self
, event
=None):
132 # put focus back to the parent window
133 if self
.parent
is not None:
134 self
.parent
.focus_set()
143 This method is called automatically to validate the data before the
144 dialog is destroyed. By default, it always validates OK.
152 This method is called automatically to process the data, *after*
153 the dialog is destroyed. By default, it does nothing.
159 # --------------------------------------------------------------------
160 # convenience dialogues
162 class _QueryDialog(Dialog
):
164 def __init__(self
, title
, prompt
,
166 minvalue
= None, maxvalue
= None,
171 parent
= Tkinter
._default
_root
174 self
.minvalue
= minvalue
175 self
.maxvalue
= maxvalue
177 self
.initialvalue
= initialvalue
179 Dialog
.__init
__(self
, parent
, title
)
185 def body(self
, master
):
187 w
= Label(master
, text
=self
.prompt
, justify
=LEFT
)
188 w
.grid(row
=0, padx
=5, sticky
=W
)
190 self
.entry
= Entry(master
, name
="entry")
191 self
.entry
.grid(row
=1, padx
=5, sticky
=W
+E
)
193 if self
.initialvalue
:
194 self
.entry
.insert(0, self
.initialvalue
)
195 self
.entry
.select_range(0, END
)
204 result
= self
.getresult()
206 tkMessageBox
.showwarning(
208 self
.errormessage
+ "\nPlease try again",
213 if self
.minvalue
is not None and result
< self
.minvalue
:
214 tkMessageBox
.showwarning(
216 "The allowed minimum value is %s. "
217 "Please try again." % self
.minvalue
,
222 if self
.maxvalue
is not None and result
> self
.maxvalue
:
223 tkMessageBox
.showwarning(
225 "The allowed maximum value is %s. "
226 "Please try again." % self
.maxvalue
,
236 class _QueryInteger(_QueryDialog
):
237 errormessage
= "Not an integer."
239 return int(self
.entry
.get())
241 def askinteger(title
, prompt
, **kw
):
242 '''get an integer from the user
246 title -- the dialog title
247 prompt -- the label text
248 **kw -- see SimpleDialog class
250 Return value is an integer
252 d
= apply(_QueryInteger
, (title
, prompt
), kw
)
255 class _QueryFloat(_QueryDialog
):
256 errormessage
= "Not a floating point value."
258 return float(self
.entry
.get())
260 def askfloat(title
, prompt
, **kw
):
261 '''get a float from the user
265 title -- the dialog title
266 prompt -- the label text
267 **kw -- see SimpleDialog class
269 Return value is a float
271 d
= apply(_QueryFloat
, (title
, prompt
), kw
)
274 class _QueryString(_QueryDialog
):
275 def __init__(self
, *args
, **kw
):
276 if kw
.has_key("show"):
277 self
.__show
= kw
["show"]
281 _QueryDialog
.__init
__(self
, *args
, **kw
)
283 def body(self
, master
):
284 entry
= _QueryDialog
.body(self
, master
)
285 if self
.__show
is not None:
286 entry
.configure(show
=self
.__show
)
290 return self
.entry
.get()
292 def askstring(title
, prompt
, **kw
):
293 '''get a string from the user
297 title -- the dialog title
298 prompt -- the label text
299 **kw -- see SimpleDialog class
301 Return value is a string
303 d
= apply(_QueryString
, (title
, prompt
), kw
)
306 if __name__
== "__main__":
311 print askinteger("Spam", "Egg count", initialvalue
=12*12)
312 print askfloat("Spam", "Egg weight\n(in tons)", minvalue
=1, maxvalue
=100)
313 print askstring("Spam", "Egg label")