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
, borderwidth
=1)
21 top
.pack(side
=TOP
, fill
=BOTH
)
22 bot
= Frame(w
, relief
=RAISED
, borderwidth
=1)
23 bot
.pack(side
=BOTTOM
, fill
=BOTH
)
25 # 2. Fill the top part with the bitmap and message.
27 msg
= Message(top
, width
='3i', text
=text
,
28 font
='-Adobe-Times-Medium-R-Normal-*-180-*')
29 msg
.pack(side
=RIGHT
, expand
=1, fill
=BOTH
, padx
='3m', pady
='3m')
31 bm
= Label(top
, bitmap
=bitmap
)
32 bm
.pack(side
=LEFT
, padx
='3m', pady
='3m')
34 # 3. Create a row of buttons at the bottom of the dialog.
40 b
= Button(bot
, text
=but
, command
=lambda v
=var
,i
=i
: v
.set(i
))
43 bd
= Frame(bot
, relief
=SUNKEN
, borderwidth
=1)
44 bd
.pack(side
=LEFT
, expand
=1, padx
='3m', pady
='2m')
46 b
.pack (in_
=bd
, side
=LEFT
,
47 padx
='2m', pady
='2m', ipadx
='2m', ipady
='1m')
49 b
.pack (side
=LEFT
, expand
=1,
50 padx
='3m', pady
='3m', ipadx
='2m', ipady
='1m')
53 # 4. Set up a binding for <Return>, if there's a default,
54 # set a grab, and claim the focus too.
58 lambda e
, b
=buttons
[default
], v
=var
, i
=default
:
62 oldFocus
= w
.focus_get()
66 # 5. Wait for the user to respond, then restore the focus
67 # and return the index of the selected button.
71 if oldFocus
: oldFocus
.focus_set()
74 # The rest is the test program.
77 i
= dialog(mainWidget
,
79 "The file server isn't responding right now; "
84 print 'pressed button', i
85 i
= dialog(mainWidget
,
87 'File "tcl.h" has been modified since '
88 'the last time it was saved. '
89 'Do you want to save it before exiting the application?',
95 print 'pressed button', i
101 Pack
.config(mainWidget
)
102 start
= Button(mainWidget
, text
='Press Here To Start', command
=go
)
104 endit
= Button(mainWidget
, text
="Exit", command
=sys
.exit
)
105 endit
.pack(fill
=BOTH
)
106 mainWidget
.mainloop()
108 if __name__
== '__main__':