3 # A Python function that generates dialog boxes with a text message,
4 # optional bitmap, and any number of buttons.
5 # Cf. Ousterhout, Tcl and the Tk Toolkit, Figs. 27.2-3, pp. 269-270.
11 def dialog(master
, title
, text
, bitmap
, default
, *args
):
13 # 1. Create the top-level window and divide it into top
16 w
= Toplevel(master
, {'class': 'Dialog'})
20 top
= Frame(w
, {'relief': 'raised', 'bd': 1,
21 Pack
: {'side': 'top', 'fill': 'both'}})
22 bot
= Frame(w
, {'relief': 'raised', 'bd': 1,
23 Pack
: {'side': 'bottom', 'fill': 'both'}})
25 # 2. Fill the top part with the bitmap and message.
30 'font': '-Adobe-Times-Medium-R-Normal-*-180-*',
31 Pack
: {'side': 'right', 'expand': 1,
33 'padx': '3m', 'pady': '3m'}})
35 bm
= Label(top
, {'bitmap': bitmap
,
36 Pack
: {'side': 'left',
37 'padx': '3m', 'pady': '3m'}})
39 # 3. Create a row of buttons at the bottom of the dialog.
44 b
= Button(bot
, {'text': but
,
45 'command': ('set', 'button', i
)})
48 bd
= Frame(bot
, {'relief': 'sunken', 'bd': 1,
49 Pack
: {'side': 'left', 'expand': 1,
50 'padx': '3m', 'pady': '2m'}})
52 b
.pack ({'in': bd
, 'side': 'left',
53 'padx': '2m', 'pady': '2m',
54 'ipadx': '2m', 'ipady': '1m'})
56 b
.pack ({'side': 'left', 'expand': 1,
57 'padx': '3m', 'pady': '3m',
58 'ipady': '2m', 'ipady': '1m'})
61 # 4. Set up a binding for <Return>, if there's a default,
62 # set a grab, and claim the focus too.
66 lambda e
, b
=buttons
[default
], i
=default
:
68 b
.setvar('button', i
)))
70 oldFocus
= w
.tk
.call('focus') # XXX
74 # 5. Wait for the user to respond, then restore the focus
75 # and return the index of the selected button.
79 w
.tk
.call('focus', oldFocus
) # XXX
80 return w
.getint(w
.getvar('button'))
82 def strdialog(master
, title
, text
, bitmap
, default
, *args
):
84 # 1. Create the top-level window and divide it into top
87 w
= Toplevel(master
, {'class': 'Dialog'})
91 top
= Frame(w
, {'relief': 'raised', 'bd': 1,
92 Pack
: {'side': 'top', 'fill': 'both'}})
94 bot
= Frame(w
, {'relief': 'raised', 'bd': 1,
95 Pack
: {'side': 'bottom', 'fill': 'both'}})
97 # 2. Fill the top part with the bitmap, message and input field.
100 bm
= Label(top
, {'bitmap': bitmap
,
101 Pack
: {'side': 'left',
102 'padx': '3m', 'pady': '3m'}})
107 'font': '-Adobe-Times-Medium-R-Normal-*-180-*',
108 Pack
: {'side': 'left',
110 'padx': '3m', 'pady': '3m'}})
117 'padx':'3m', 'pady':'3m'}})
118 # 3. Create a row of buttons at the bottom of the dialog.
123 b
= Button(bot
, {'text': but
,
124 'command': ('set', 'button', i
)})
127 bd
= Frame(bot
, {'relief': 'sunken', 'bd': 1,
128 Pack
: {'side': 'left', 'expand': 1,
129 'padx': '3m', 'pady': '2m'}})
131 b
.pack ({'in': bd
, 'side': 'left',
132 'padx': '2m', 'pady': '2m',
133 'ipadx': '2m', 'ipady': '1m'})
135 b
.pack ({'side': 'left', 'expand': 1,
136 'padx': '3m', 'pady': '3m',
137 'ipady': '2m', 'ipady': '1m'})
140 # 4. Set up a binding for <Return>, if there's a default,
141 # set a grab, and claim the focus too.
144 w
.bind('<Return>', lambda arg
, top
=top
: top
.setvar('button', 0))
145 field
.bind('<Return>', lambda arg
, top
=top
: top
.setvar('button', 0))
148 lambda e
, b
=buttons
[default
], i
=default
:
150 b
.setvar('button', i
)))
151 field
.bind('<Return>',
152 lambda e
, b
=buttons
[default
], i
=default
:
154 b
.setvar('button', i
)))
156 oldFocus
= w
.tk
.call('focus') # XXX
160 # 5. Wait for the user to respond, then restore the focus
161 # and return the index of the selected button.
166 w
.tk
.call('focus', oldFocus
) # XXX
168 return v
, w
.getint(w
.getvar('button'))
173 i
= dialog(mainWidget
, 'Message', str, '', 0, 'OK')
176 i
= dialog(mainWidget
, 'Question', str, '', 0, 'No', 'Yes')
180 i
= dialog(mainWidget
, 'Question', str, '', 0, 'Cancel', 'No', 'Yes')
184 i
= strdialog(mainWidget
, 'Question', str, '', 0)
187 def askfile(str): # XXXX For now...
188 i
= strdialog(mainWidget
, 'Question', str, '', 0)
191 # The rest is the test program.
194 i
= dialog(mainWidget
,
196 "The file server isn't responding right now; "
201 print 'pressed button', i
202 i
= dialog(mainWidget
,
204 'File "tcl.h" has been modified since '
205 'the last time it was saved. '
206 'Do you want to save it before exiting the application?',
212 print 'pressed button', i
213 print message('Test of message')
214 print askyn('Test of yes/no')
215 print askync('Test of yes/no/cancel')
216 print askstr('Type a string:')
217 print strdialog(mainWidget
, 'Question', 'Another string:', '',
218 0, 'Save', 'Save as text')
223 Pack
.config(mainWidget
)
224 start
= Button(mainWidget
,
225 {'text': 'Press Here To Start', 'command': _go
})
227 endit
= Button(mainWidget
,
230 Pack
: {'fill' : 'both'}})
231 mainWidget
.mainloop()
233 if __name__
== '__main__':