2 # An Introduction to Tkinter
5 # Copyright (c) 1997 by Fredrik Lundh
7 # fredrik@pythonware.com
8 # http://www.pythonware.com
11 # --------------------------------------------------------------------
17 class Dialog(Toplevel
):
19 def __init__(self
, parent
, title
= None):
21 Toplevel
.__init
__(self
, parent
)
22 self
.transient(parent
)
32 self
.initial_focus
= self
.body(body
)
33 body
.pack(padx
=5, pady
=5)
39 if not self
.initial_focus
:
40 self
.initial_focus
= self
42 self
.protocol("WM_DELETE_WINDOW", self
.cancel
)
44 self
.geometry("+%d+%d" % (parent
.winfo_rootx()+50,
45 parent
.winfo_rooty()+50))
47 self
.initial_focus
.focus_set()
49 self
.wait_window(self
)
54 def body(self
, master
):
55 # create dialog body. return widget that should have
56 # initial focus. this method should be overridden
61 # add standard button box. override if you don't want the
66 w
= Button(box
, text
="OK", width
=10, command
=self
.ok
, default
=ACTIVE
)
67 w
.pack(side
=LEFT
, padx
=5, pady
=5)
68 w
= Button(box
, text
="Cancel", width
=10, command
=self
.cancel
)
69 w
.pack(side
=LEFT
, padx
=5, pady
=5)
71 self
.bind("<Return>", self
.ok
)
72 self
.bind("<Escape>", self
.cancel
)
77 # standard button semantics
79 def ok(self
, event
=None):
81 if not self
.validate():
82 self
.initial_focus
.focus_set() # put focus back
86 self
.update_idletasks()
92 def cancel(self
, event
=None):
94 # put focus back to the parent window
95 self
.parent
.focus_set()
110 # --------------------------------------------------------------------
111 # convenience dialogues
115 class _QueryDialog(Dialog
):
117 def __init__(self
, title
, prompt
,
119 minvalue
= None, maxvalue
= None,
124 parent
= Tkinter
._default
_root
127 self
.minvalue
= minvalue
128 self
.maxvalue
= maxvalue
130 self
.initialvalue
= initialvalue
132 Dialog
.__init
__(self
, parent
, title
)
134 def body(self
, master
):
136 w
= Label(master
, text
=self
.prompt
, justify
=LEFT
)
137 w
.grid(row
=0, padx
=5, sticky
=W
)
139 self
.entry
= Entry(master
, name
="entry")
140 self
.entry
.grid(row
=1, padx
=5, sticky
=W
+E
)
142 if self
.initialvalue
:
143 self
.entry
.insert(0, self
.initialvalue
)
144 self
.entry
.select_range(0, END
)
153 result
= self
.getresult()
155 tkMessageBox
.showwarning(
157 self
.errormessage
+ "\nPlease try again",
162 if self
.minvalue
is not None and result
< self
.minvalue
:
163 tkMessageBox
.showwarning(
165 "The allowed minimum value is %s. "
166 "Please try again." % self
.minvalue
,
171 if self
.maxvalue
is not None and result
> self
.maxvalue
:
172 tkMessageBox
.showwarning(
174 "The allowed maximum value is %s. "
175 "Please try again." % self
.maxvalue
,
185 class _QueryInteger(_QueryDialog
):
186 errormessage
= "Not an integer."
188 return string
.atoi(self
.entry
.get())
190 def askinteger(title
, prompt
, **kw
):
191 d
= apply(_QueryInteger
, (title
, prompt
), kw
)
194 class _QueryFloat(_QueryDialog
):
195 errormessage
= "Not a floating point value."
197 return string
.atof(self
.entry
.get())
199 def askfloat(title
, prompt
, **kw
):
200 d
= apply(_QueryFloat
, (title
, prompt
), kw
)
203 class _QueryString(_QueryDialog
):
205 return self
.entry
.get()
207 def askstring(title
, prompt
, **kw
):
208 d
= apply(_QueryString
, (title
, prompt
), kw
)
211 if __name__
== "__main__":
216 print askinteger("Spam", "Egg count", initialvalue
=12*12)
217 print askfloat("Spam", "Egg weight\n(in tons)", minvalue
=1, maxvalue
=100)
218 print askstring("Spam", "Egg label")