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