Files for 2.1b1 distribution.
[python/dscho.git] / Lib / lib-tk / tkSimpleDialog.py
blob15ff544cb63bea8667b23f478cf500e4be81bfde
2 # An Introduction to Tkinter
3 # tkSimpleDialog.py
5 # Copyright (c) 1997 by Fredrik Lundh
7 # fredrik@pythonware.com
8 # http://www.pythonware.com
11 # --------------------------------------------------------------------
12 # dialog base class
14 '''Dialog boxes
16 This module handles dialog boxes. It contains the following
17 public symbols:
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
26 '''
28 from Tkinter import *
29 import os
31 class Dialog(Toplevel):
33 '''Class to open dialogs.
35 This class is intended as a base class for custom dialogs
36 '''
38 def __init__(self, parent, title = None):
40 '''Initialize a dialog.
42 Arguments:
44 parent -- a parent window (the application window)
46 title -- the dialog title
47 '''
48 Toplevel.__init__(self, parent)
49 self.transient(parent)
51 if title:
52 self.title(title)
54 self.parent = parent
56 self.result = None
58 body = Frame(self)
59 self.initial_focus = self.body(body)
60 body.pack(padx=5, pady=5)
62 self.buttonbox()
64 self.grab_set()
66 if not self.initial_focus:
67 self.initial_focus = self
69 self.protocol("WM_DELETE_WINDOW", self.cancel)
71 self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
72 parent.winfo_rooty()+50))
74 self.initial_focus.focus_set()
76 self.wait_window(self)
78 def destroy(self):
79 '''Destroy the window'''
80 self.initial_focus = None
81 Toplevel.destroy(self)
84 # construction hooks
86 def body(self, master):
87 '''create dialog body.
89 return widget that should have initial focus.
90 This method should be overridden, and is called
91 by the __init__ method.
92 '''
93 pass
95 def buttonbox(self):
96 '''add standard button box.
98 override if you don't want the standard buttons
99 '''
101 box = Frame(self)
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)
108 self.bind("<Return>", self.ok)
109 self.bind("<Escape>", self.cancel)
111 box.pack()
114 # standard button semantics
116 def ok(self, event=None):
118 if not self.validate():
119 self.initial_focus.focus_set() # put focus back
120 return
122 self.withdraw()
123 self.update_idletasks()
125 self.apply()
127 self.cancel()
129 def cancel(self, event=None):
131 # put focus back to the parent window
132 self.parent.focus_set()
133 self.destroy()
136 # command hooks
138 def validate(self):
139 '''validate the data
141 This method is called automatically to validate the data before the
142 dialog is destroyed. By default, it always validates OK.
145 return 1 # override
147 def apply(self):
148 '''process the data
150 This method is called automatically to process the data, *after*
151 the dialog is destroyed. By default, it does nothing.
154 pass # override
157 # --------------------------------------------------------------------
158 # convenience dialogues
160 class _QueryDialog(Dialog):
162 def __init__(self, title, prompt,
163 initialvalue=None,
164 minvalue = None, maxvalue = None,
165 parent = None):
167 if not parent:
168 import Tkinter
169 parent = Tkinter._default_root
171 self.prompt = prompt
172 self.minvalue = minvalue
173 self.maxvalue = maxvalue
175 self.initialvalue = initialvalue
177 Dialog.__init__(self, parent, title)
179 def destroy(self):
180 self.entry = None
181 Dialog.destroy(self)
183 def body(self, master):
185 w = Label(master, text=self.prompt, justify=LEFT)
186 w.grid(row=0, padx=5, sticky=W)
188 self.entry = Entry(master, name="entry")
189 self.entry.grid(row=1, padx=5, sticky=W+E)
191 if self.initialvalue:
192 self.entry.insert(0, self.initialvalue)
193 self.entry.select_range(0, END)
195 return self.entry
197 def validate(self):
199 import tkMessageBox
201 try:
202 result = self.getresult()
203 except ValueError:
204 tkMessageBox.showwarning(
205 "Illegal value",
206 self.errormessage + "\nPlease try again",
207 parent = self
209 return 0
211 if self.minvalue is not None and result < self.minvalue:
212 tkMessageBox.showwarning(
213 "Too small",
214 "The allowed minimum value is %s. "
215 "Please try again." % self.minvalue,
216 parent = self
218 return 0
220 if self.maxvalue is not None and result > self.maxvalue:
221 tkMessageBox.showwarning(
222 "Too large",
223 "The allowed maximum value is %s. "
224 "Please try again." % self.maxvalue,
225 parent = self
227 return 0
229 self.result = result
231 return 1
234 class _QueryInteger(_QueryDialog):
235 errormessage = "Not an integer."
236 def getresult(self):
237 return int(self.entry.get())
239 def askinteger(title, prompt, **kw):
240 '''get an integer from the user
242 Arguments:
244 title -- the dialog title
245 prompt -- the label text
246 **kw -- see SimpleDialog class
248 Return value is an integer
250 d = apply(_QueryInteger, (title, prompt), kw)
251 return d.result
253 class _QueryFloat(_QueryDialog):
254 errormessage = "Not a floating point value."
255 def getresult(self):
256 return float(self.entry.get())
258 def askfloat(title, prompt, **kw):
259 '''get a float from the user
261 Arguments:
263 title -- the dialog title
264 prompt -- the label text
265 **kw -- see SimpleDialog class
267 Return value is a float
269 d = apply(_QueryFloat, (title, prompt), kw)
270 return d.result
272 class _QueryString(_QueryDialog):
273 def getresult(self):
274 return self.entry.get()
276 def askstring(title, prompt, **kw):
277 '''get a string from the user
279 Arguments:
281 title -- the dialog title
282 prompt -- the label text
283 **kw -- see SimpleDialog class
285 Return value is a string
287 d = apply(_QueryString, (title, prompt), kw)
288 return d.result
290 if __name__ == "__main__":
292 root = Tk()
293 root.update()
295 print askinteger("Spam", "Egg count", initialvalue=12*12)
296 print askfloat("Spam", "Egg weight\n(in tons)", minvalue=1, maxvalue=100)
297 print askstring("Spam", "Egg label")