1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# An Introduction to Tkinter
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# tkSimpleDialog.py
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Copyright (c) 1997 by Fredrik Lundh
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# fredrik@pythonware.com
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# http://www.pythonware.com
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# --------------------------------------------------------------------
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# dialog base class
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep'''Dialog boxes
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThis module handles dialog boxes. It contains the following
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppublic symbols:
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepDialog -- a base class for dialogs
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepaskinteger -- get an integer from the user
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepaskfloat -- get a float from the user
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepaskstring -- get a string from the user
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep'''
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom Tkinter import *
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass Dialog(Toplevel):
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '''Class to open dialogs.
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    This class is intended as a base class for custom dialogs
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '''
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, parent, title = None):
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        '''Initialize a dialog.
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Arguments:
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            parent -- a parent window (the application window)
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            title -- the dialog title
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        '''
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Toplevel.__init__(self, parent)
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.withdraw() # remain invisible for now
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # If the master is not viewable, don't
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # make the child transient, or else it
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # would be opened withdrawn
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if parent.winfo_viewable():
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.transient(parent)
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if title:
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.title(title)
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.parent = parent
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.result = None
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        body = Frame(self)
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.initial_focus = self.body(body)
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        body.pack(padx=5, pady=5)
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.buttonbox()
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if not self.initial_focus:
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.initial_focus = self
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.protocol("WM_DELETE_WINDOW", self.cancel)
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self.parent is not None:
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                      parent.winfo_rooty()+50))
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.deiconify() # become visibile now
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.initial_focus.focus_set()
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # wait for window to appear on screen before calling grab_set
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.wait_visibility()
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.grab_set()
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.wait_window(self)
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def destroy(self):
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        '''Destroy the window'''
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.initial_focus = None
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Toplevel.destroy(self)
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    #
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # construction hooks
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def body(self, master):
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        '''create dialog body.
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return widget that should have initial focus.
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        This method should be overridden, and is called
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        by the __init__ method.
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        '''
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def buttonbox(self):
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        '''add standard button box.
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        override if you do not want the standard buttons
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        '''
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        box = Frame(self)
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE)
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        w.pack(side=LEFT, padx=5, pady=5)
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        w = Button(box, text="Cancel", width=10, command=self.cancel)
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        w.pack(side=LEFT, padx=5, pady=5)
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.bind("<Return>", self.ok)
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.bind("<Escape>", self.cancel)
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        box.pack()
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    #
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # standard button semantics
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def ok(self, event=None):
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if not self.validate():
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.initial_focus.focus_set() # put focus back
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.withdraw()
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.update_idletasks()
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.apply()
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        finally:
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cancel()
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def cancel(self, event=None):
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # put focus back to the parent window
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self.parent is not None:
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.parent.focus_set()
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.destroy()
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    #
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # command hooks
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def validate(self):
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        '''validate the data
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        This method is called automatically to validate the data before the
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dialog is destroyed. By default, it always validates OK.
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        '''
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return 1 # override
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def apply(self):
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        '''process the data
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        This method is called automatically to process the data, *after*
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        the dialog is destroyed. By default, it does nothing.
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        '''
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass # override
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# --------------------------------------------------------------------
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# convenience dialogues
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass _QueryDialog(Dialog):
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, title, prompt,
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                 initialvalue=None,
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                 minvalue = None, maxvalue = None,
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                 parent = None):
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if not parent:
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            import Tkinter
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            parent = Tkinter._default_root
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.prompt   = prompt
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.minvalue = minvalue
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.maxvalue = maxvalue
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.initialvalue = initialvalue
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Dialog.__init__(self, parent, title)
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def destroy(self):
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.entry = None
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Dialog.destroy(self)
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def body(self, master):
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        w = Label(master, text=self.prompt, justify=LEFT)
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        w.grid(row=0, padx=5, sticky=W)
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.entry = Entry(master, name="entry")
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.entry.grid(row=1, padx=5, sticky=W+E)
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self.initialvalue is not None:
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.entry.insert(0, self.initialvalue)
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.entry.select_range(0, END)
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self.entry
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def validate(self):
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import tkMessageBox
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            result = self.getresult()
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ValueError:
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            tkMessageBox.showwarning(
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                "Illegal value",
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.errormessage + "\nPlease try again",
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                parent = self
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            )
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return 0
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self.minvalue is not None and result < self.minvalue:
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            tkMessageBox.showwarning(
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                "Too small",
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                "The allowed minimum value is %s. "
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                "Please try again." % self.minvalue,
228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                parent = self
229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            )
230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return 0
231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self.maxvalue is not None and result > self.maxvalue:
233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            tkMessageBox.showwarning(
234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                "Too large",
235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                "The allowed maximum value is %s. "
236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                "Please try again." % self.maxvalue,
237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                parent = self
238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            )
239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return 0
240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.result = result
242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return 1
244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass _QueryInteger(_QueryDialog):
247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errormessage = "Not an integer."
248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def getresult(self):
249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return int(self.entry.get())
250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef askinteger(title, prompt, **kw):
252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '''get an integer from the user
253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Arguments:
255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        title -- the dialog title
257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        prompt -- the label text
258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        **kw -- see SimpleDialog class
259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Return value is an integer
261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '''
262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    d = _QueryInteger(title, prompt, **kw)
263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return d.result
264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass _QueryFloat(_QueryDialog):
266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    errormessage = "Not a floating point value."
267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def getresult(self):
268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return float(self.entry.get())
269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef askfloat(title, prompt, **kw):
271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '''get a float from the user
272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Arguments:
274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        title -- the dialog title
276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        prompt -- the label text
277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        **kw -- see SimpleDialog class
278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Return value is a float
280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '''
281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    d = _QueryFloat(title, prompt, **kw)
282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return d.result
283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass _QueryString(_QueryDialog):
285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, *args, **kw):
286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if "show" in kw:
287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.__show = kw["show"]
288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            del kw["show"]
289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.__show = None
291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        _QueryDialog.__init__(self, *args, **kw)
292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def body(self, master):
294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        entry = _QueryDialog.body(self, master)
295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self.__show is not None:
296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            entry.configure(show=self.__show)
297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return entry
298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def getresult(self):
300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self.entry.get()
301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef askstring(title, prompt, **kw):
303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '''get a string from the user
304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Arguments:
306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        title -- the dialog title
308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        prompt -- the label text
309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        **kw -- see SimpleDialog class
310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Return value is a string
312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '''
313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    d = _QueryString(title, prompt, **kw)
314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return d.result
315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__":
317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    root = Tk()
319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    root.update()
320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    print askinteger("Spam", "Egg count", initialvalue=12*12)
322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    print askfloat("Spam", "Egg weight\n(in tons)", minvalue=1, maxvalue=100)
323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    print askstring("Spam", "Egg label")
324