Class around PixMap objects that allows more python-like access. By Joe Strout.
[python/dscho.git] / Lib / lib-tk / tkSimpleDialog.py
blob1e7ed4668cd516dfd7c3fd4324dc3cee62414486
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 from Tkinter import *
15 import os
17 class Dialog(Toplevel):
19 def __init__(self, parent, title = None):
21 Toplevel.__init__(self, parent)
22 self.transient(parent)
24 if title:
25 self.title(title)
27 self.parent = parent
29 self.result = None
31 body = Frame(self)
32 self.initial_focus = self.body(body)
33 body.pack(padx=5, pady=5)
35 self.buttonbox()
37 self.grab_set()
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)
52 # construction hooks
54 def body(self, master):
55 # create dialog body. return widget that should have
56 # initial focus. this method should be overridden
58 pass
60 def buttonbox(self):
61 # add standard button box. override if you don't want the
62 # standard buttons
64 box = Frame(self)
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)
74 box.pack()
77 # standard button semantics
79 def ok(self, event=None):
81 if not self.validate():
82 self.initial_focus.focus_set() # put focus back
83 return
85 self.withdraw()
86 self.update_idletasks()
88 self.apply()
90 self.cancel()
92 def cancel(self, event=None):
94 # put focus back to the parent window
95 self.parent.focus_set()
96 self.destroy()
99 # command hooks
101 def validate(self):
103 return 1 # override
105 def apply(self):
107 pass # override
110 # --------------------------------------------------------------------
111 # convenience dialogues
113 import string
115 class _QueryDialog(Dialog):
117 def __init__(self, title, prompt,
118 initialvalue=None,
119 minvalue = None, maxvalue = None,
120 parent = None):
122 if not parent:
123 import Tkinter
124 parent = Tkinter._default_root
126 self.prompt = prompt
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)
146 return self.entry
148 def validate(self):
150 import tkMessageBox
152 try:
153 result = self.getresult()
154 except ValueError:
155 tkMessageBox.showwarning(
156 "Illegal value",
157 self.errormessage + "\nPlease try again",
158 parent = self
160 return 0
162 if self.minvalue is not None and result < self.minvalue:
163 tkMessageBox.showwarning(
164 "Too small",
165 "The allowed minimum value is %s. "
166 "Please try again." % self.minvalue,
167 parent = self
169 return 0
171 if self.maxvalue is not None and result > self.maxvalue:
172 tkMessageBox.showwarning(
173 "Too large",
174 "The allowed maximum value is %s. "
175 "Please try again." % self.maxvalue,
176 parent = self
178 return 0
180 self.result = result
182 return 1
185 class _QueryInteger(_QueryDialog):
186 errormessage = "Not an integer."
187 def getresult(self):
188 return string.atoi(self.entry.get())
190 def askinteger(title, prompt, **kw):
191 d = apply(_QueryInteger, (title, prompt), kw)
192 return d.result
194 class _QueryFloat(_QueryDialog):
195 errormessage = "Not a floating point value."
196 def getresult(self):
197 return string.atof(self.entry.get())
199 def askfloat(title, prompt, **kw):
200 d = apply(_QueryFloat, (title, prompt), kw)
201 return d.result
203 class _QueryString(_QueryDialog):
204 def getresult(self):
205 return self.entry.get()
207 def askstring(title, prompt, **kw):
208 d = apply(_QueryString, (title, prompt), kw)
209 return d.result
211 if __name__ == "__main__":
213 root = Tk()
214 root.update()
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")