annotate Lib/lib-tk/tkSimpleDialog.py @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents 22ded309648b
children 0f135affabf2 89f2e8da0f0f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 #
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # An Introduction to Tkinter
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # tkSimpleDialog.py
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # Copyright (c) 1997 by Fredrik Lundh
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # fredrik@pythonware.com
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # http://www.pythonware.com
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # --------------------------------------------------------------------
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # dialog base class
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
14 '''Dialog boxes
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
15
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
16 This module handles dialog boxes. It contains the following
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
17 public symbols:
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
18
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
19 Dialog -- a base class for dialogs
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
20
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
21 askinteger -- get an integer from the user
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
22
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
23 askfloat -- get a float from the user
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
24
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
25 askstring -- get a string from the user
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
26 '''
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
27
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 from Tkinter import *
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 import os
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 class Dialog(Toplevel):
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
33 '''Class to open dialogs.
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
34
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
35 This class is intended as a base class for custom dialogs
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
36 '''
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
37
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 def __init__(self, parent, title = None):
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
40 '''Initialize a dialog.
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
41
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
42 Arguments:
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
43
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
44 parent -- a parent window (the application window)
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
45
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
46 title -- the dialog title
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
47 '''
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 Toplevel.__init__(self, parent)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 self.transient(parent)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 if title:
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 self.title(title)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 self.parent = parent
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 self.result = None
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 body = Frame(self)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 self.initial_focus = self.body(body)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 body.pack(padx=5, pady=5)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 self.buttonbox()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
32940
22ded309648b Wait for visibility before setting a grab. Fixes #946153.
Martin v. Löwis <martin@v.loewis.de>
parents: 28513
diff changeset
64 self.wait_visibility() # window needs to be visible for the grab
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 self.grab_set()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 if not self.initial_focus:
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 self.initial_focus = self
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 self.protocol("WM_DELETE_WINDOW", self.cancel)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
21259
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
72 if self.parent is not None:
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
73 self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
74 parent.winfo_rooty()+50))
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 self.initial_focus.focus_set()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 self.wait_window(self)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
11051
27fbb08f8c38 Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents: 9330
diff changeset
80 def destroy(self):
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
81 '''Destroy the window'''
11051
27fbb08f8c38 Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents: 9330
diff changeset
82 self.initial_focus = None
27fbb08f8c38 Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents: 9330
diff changeset
83 Toplevel.destroy(self)
27fbb08f8c38 Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents: 9330
diff changeset
84
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 #
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 # construction hooks
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 def body(self, master):
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
89 '''create dialog body.
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 11845
diff changeset
91 return widget that should have initial focus.
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
92 This method should be overridden, and is called
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
93 by the __init__ method.
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
94 '''
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 pass
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 def buttonbox(self):
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 11845
diff changeset
98 '''add standard button box.
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
99
21259
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
100 override if you do not want the standard buttons
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
101 '''
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 11845
diff changeset
102
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 box = Frame(self)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 w.pack(side=LEFT, padx=5, pady=5)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 w = Button(box, text="Cancel", width=10, command=self.cancel)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 w.pack(side=LEFT, padx=5, pady=5)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 self.bind("<Return>", self.ok)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 self.bind("<Escape>", self.cancel)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 box.pack()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 #
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 # standard button semantics
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 def ok(self, event=None):
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 if not self.validate():
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 self.initial_focus.focus_set() # put focus back
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 return
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 self.withdraw()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 self.update_idletasks()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 self.apply()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 self.cancel()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 def cancel(self, event=None):
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 # put focus back to the parent window
21259
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
134 if self.parent is not None:
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
135 self.parent.focus_set()
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 self.destroy()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 #
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 # command hooks
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 def validate(self):
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
142 '''validate the data
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
143
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 11845
diff changeset
144 This method is called automatically to validate the data before the
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
145 dialog is destroyed. By default, it always validates OK.
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
146 '''
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 return 1 # override
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 def apply(self):
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
151 '''process the data
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
152
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
153 This method is called automatically to process the data, *after*
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
154 the dialog is destroyed. By default, it does nothing.
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
155 '''
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 pass # override
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 # --------------------------------------------------------------------
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 # convenience dialogues
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 class _QueryDialog(Dialog):
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 def __init__(self, title, prompt,
6013
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
166 initialvalue=None,
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 minvalue = None, maxvalue = None,
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 parent = None):
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 if not parent:
9330
fd5a4309bfdc Only reference Tkinter._default_root when needed.
Guido van Rossum <guido@python.org>
parents: 6013
diff changeset
171 import Tkinter
fd5a4309bfdc Only reference Tkinter._default_root when needed.
Guido van Rossum <guido@python.org>
parents: 6013
diff changeset
172 parent = Tkinter._default_root
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 self.prompt = prompt
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 self.minvalue = minvalue
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 self.maxvalue = maxvalue
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
177
6013
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
178 self.initialvalue = initialvalue
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
179
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 Dialog.__init__(self, parent, title)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
11051
27fbb08f8c38 Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents: 9330
diff changeset
182 def destroy(self):
27fbb08f8c38 Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents: 9330
diff changeset
183 self.entry = None
27fbb08f8c38 Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents: 9330
diff changeset
184 Dialog.destroy(self)
27fbb08f8c38 Break some cycles when the widget is destroyed.
Guido van Rossum <guido@python.org>
parents: 9330
diff changeset
185
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 def body(self, master):
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
6013
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
188 w = Label(master, text=self.prompt, justify=LEFT)
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 w.grid(row=0, padx=5, sticky=W)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 self.entry = Entry(master, name="entry")
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 self.entry.grid(row=1, padx=5, sticky=W+E)
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
6013
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
194 if self.initialvalue:
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
195 self.entry.insert(0, self.initialvalue)
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
196 self.entry.select_range(0, END)
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
197
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 return self.entry
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 def validate(self):
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 import tkMessageBox
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
203
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 try:
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 result = self.getresult()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 except ValueError:
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 tkMessageBox.showwarning(
6013
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
208 "Illegal value",
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 self.errormessage + "\nPlease try again",
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 parent = self
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 )
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 return 0
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
213
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 if self.minvalue is not None and result < self.minvalue:
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 tkMessageBox.showwarning(
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 "Too small",
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 "The allowed minimum value is %s. "
6013
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
218 "Please try again." % self.minvalue,
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 parent = self
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 )
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 return 0
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 if self.maxvalue is not None and result > self.maxvalue:
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 tkMessageBox.showwarning(
6013
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
225 "Too large",
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 "The allowed maximum value is %s. "
6013
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
227 "Please try again." % self.maxvalue,
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 parent = self
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 )
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 return 0
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 11845
diff changeset
231
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 self.result = result
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
233
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 return 1
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
235
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 class _QueryInteger(_QueryDialog):
6013
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
238 errormessage = "Not an integer."
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 def getresult(self):
16517
eee67a84e5fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15308
diff changeset
240 return int(self.entry.get())
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
241
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 def askinteger(title, prompt, **kw):
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
243 '''get an integer from the user
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
244
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
245 Arguments:
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
246
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
247 title -- the dialog title
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
248 prompt -- the label text
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
249 **kw -- see SimpleDialog class
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
250
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
251 Return value is an integer
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
252 '''
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 21259
diff changeset
253 d = _QueryInteger(title, prompt, **kw)
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 return d.result
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 class _QueryFloat(_QueryDialog):
6013
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
257 errormessage = "Not a floating point value."
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 def getresult(self):
16517
eee67a84e5fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15308
diff changeset
259 return float(self.entry.get())
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
260
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 def askfloat(title, prompt, **kw):
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
262 '''get a float from the user
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
263
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
264 Arguments:
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
265
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
266 title -- the dialog title
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
267 prompt -- the label text
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
268 **kw -- see SimpleDialog class
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
269
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
270 Return value is a float
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
271 '''
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 21259
diff changeset
272 d = _QueryFloat(title, prompt, **kw)
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 return d.result
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 class _QueryString(_QueryDialog):
21259
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
276 def __init__(self, *args, **kw):
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
277 if kw.has_key("show"):
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
278 self.__show = kw["show"]
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
279 del kw["show"]
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
280 else:
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
281 self.__show = None
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
282 _QueryDialog.__init__(self, *args, **kw)
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
283
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
284 def body(self, master):
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
285 entry = _QueryDialog.body(self, master)
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
286 if self.__show is not None:
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
287 entry.configure(show=self.__show)
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
288 return entry
102d18ea02cf Be more careful about accessing attributes of the parent: if Tk has not been
Fred Drake <fdrake@acm.org>
parents: 16517
diff changeset
289
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 def getresult(self):
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 return self.entry.get()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
292
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 def askstring(title, prompt, **kw):
11845
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
294 '''get a string from the user
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
295
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
296 Arguments:
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
297
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
298 title -- the dialog title
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
299 prompt -- the label text
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
300 **kw -- see SimpleDialog class
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
301
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
302 Return value is a string
e56780b7436a Moshe Zadka:
Guido van Rossum <guido@python.org>
parents: 11051
diff changeset
303 '''
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 21259
diff changeset
304 d = _QueryString(title, prompt, **kw)
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 return d.result
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
306
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 11845
diff changeset
307 if __name__ == "__main__":
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
308
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 root = Tk()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 root.update()
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
311
6013
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
312 print askinteger("Spam", "Egg count", initialvalue=12*12)
0b5a23ea7515 Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents: 5990
diff changeset
313 print askfloat("Spam", "Egg weight\n(in tons)", minvalue=1, maxvalue=100)
5990
503500652bac New dialog from Fred Lundh
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 print askstring("Spam", "Egg label")