blob: 27cddf0c82ec34e3134a91547af9dc47ed4816f0 [file] [log] [blame]
Guido van Rossum4450bab1994-10-11 14:57:03 +00001#! /usr/local/bin/python
Guido van Rossum18468821994-06-20 07:49:28 +00002
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.
6
7from Tkinter import *
8
9def dialog(master, title, text, bitmap, default, *args):
10
11 # 1. Create the top-level window and divide it into top
12 # and bottom parts.
13
14 w = Toplevel(master, {'class': 'Dialog'})
Guido van Rossum18468821994-06-20 07:49:28 +000015 w.title(title)
16 w.iconname('Dialog')
17
18 top = Frame(w, {'relief': 'raised', 'bd': 1,
19 Pack: {'side': 'top', 'fill': 'both'}})
20 bot = Frame(w, {'relief': 'raised', 'bd': 1,
21 Pack: {'side': 'bottom', 'fill': 'both'}})
22
23 # 2. Fill the top part with the bitmap and message.
24
25 msg = Message(top,
26 {'width': '3i',
27 'text': text,
28 'font': '-Adobe-Times-Medium-R-Normal-*-180-*',
29 Pack: {'side': 'right', 'expand': 1,
30 'fill': 'both',
31 'padx': '3m', 'pady': '3m'}})
32 if bitmap:
33 bm = Label(top, {'bitmap': bitmap,
34 Pack: {'side': 'left',
35 'padx': '3m', 'pady': '3m'}})
36
37 # 3. Create a row of buttons at the bottom of the dialog.
38
39 buttons = []
40 i = 0
41 for but in args:
42 b = Button(bot, {'text': but,
43 'command': ('set', 'button', i)})
44 buttons.append(b)
45 if i == default:
46 bd = Frame(bot, {'relief': 'sunken', 'bd': 1,
47 Pack: {'side': 'left', 'expand': 1,
48 'padx': '3m', 'pady': '2m'}})
Guido van Rossum3c1d0871994-06-20 08:11:36 +000049 b.lift()
Guido van Rossum18468821994-06-20 07:49:28 +000050 b.pack ({'in': bd, 'side': 'left',
51 'padx': '2m', 'pady': '2m',
52 'ipadx': '2m', 'ipady': '1m'})
53 else:
54 b.pack ({'side': 'left', 'expand': 1,
55 'padx': '3m', 'pady': '3m',
56 'ipady': '2m', 'ipady': '1m'})
57 i = i+1
58
59 # 4. Set up a binding for <Return>, if there's a default,
60 # set a grab, and claim the focus too.
61
62 if default >= 0:
63 w.bind('<Return>',
Guido van Rossum3c1d0871994-06-20 08:11:36 +000064 lambda e, b=buttons[default], i=default:
65 (b.flash(),
66 b.setvar('button', i)))
Guido van Rossum18468821994-06-20 07:49:28 +000067
Guido van Rossum3c1d0871994-06-20 08:11:36 +000068 oldFocus = w.tk.call('focus') # XXX
69 w.grab_set()
70 w.focus()
Guido van Rossum18468821994-06-20 07:49:28 +000071
72 # 5. Wait for the user to respond, then restore the focus
73 # and return the index of the selected button.
74
Guido van Rossum3c1d0871994-06-20 08:11:36 +000075 w.waitvar('button')
76 w.destroy()
77 w.tk.call('focus', oldFocus) # XXX
78 return w.getint(w.getvar('button'))
Guido van Rossum18468821994-06-20 07:49:28 +000079
80# The rest is the test program.
81
82def go():
83 i = dialog(mainWidget,
84 'Not Responding',
85 "The file server isn't responding right now; "
86 "I'll keep trying.",
87 '',
88 -1,
89 'OK')
90 print 'pressed button', i
91 i = dialog(mainWidget,
92 'File Modified',
93 'File "tcl.h" has been modified since '
94 'the last time it was saved. '
95 'Do you want to save it before exiting the application?',
96 'warning',
97 0,
98 'Save File',
99 'Discard Changes',
100 'Return To Editor')
101 print 'pressed button', i
102
103def test():
104 import sys
105 global mainWidget
106 mainWidget = Frame()
107 Pack.config(mainWidget)
108 start = Button(mainWidget,
109 {'text': 'Press Here To Start', 'command': go})
110 start.pack()
111 endit = Button(mainWidget,
112 {'text': 'Exit',
113 'command': 'exit',
114 Pack: {'fill' : 'both'}})
Guido van Rossum3c1d0871994-06-20 08:11:36 +0000115 mainWidget.mainloop()
Guido van Rossum18468821994-06-20 07:49:28 +0000116
117if __name__ == '__main__':
118 test()