blob: 426eca41956603688b603c95d6f43e9861cb5732 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env 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 *
Guido van Rossum89cb67b1996-07-30 18:57:18 +00008import sys
9
Guido van Rossum18468821994-06-20 07:49:28 +000010
11def dialog(master, title, text, bitmap, default, *args):
12
13 # 1. Create the top-level window and divide it into top
14 # and bottom parts.
15
Guido van Rossum89cb67b1996-07-30 18:57:18 +000016 w = Toplevel(master, class_='Dialog')
Guido van Rossum18468821994-06-20 07:49:28 +000017 w.title(title)
18 w.iconname('Dialog')
19
Guido van Rossum89cb67b1996-07-30 18:57:18 +000020 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)
Guido van Rossum18468821994-06-20 07:49:28 +000024
25 # 2. Fill the top part with the bitmap and message.
26
Guido van Rossum89cb67b1996-07-30 18:57:18 +000027 msg = Message(top, width='3i', text=text,
Tim Peters182b5ac2004-07-18 06:16:08 +000028 font='-Adobe-Times-Medium-R-Normal-*-180-*')
Guido van Rossum89cb67b1996-07-30 18:57:18 +000029 msg.pack(side=RIGHT, expand=1, fill=BOTH, padx='3m', pady='3m')
Guido van Rossum18468821994-06-20 07:49:28 +000030 if bitmap:
Tim Peters182b5ac2004-07-18 06:16:08 +000031 bm = Label(top, bitmap=bitmap)
32 bm.pack(side=LEFT, padx='3m', pady='3m')
Guido van Rossum18468821994-06-20 07:49:28 +000033
34 # 3. Create a row of buttons at the bottom of the dialog.
35
Guido van Rossum89cb67b1996-07-30 18:57:18 +000036 var = IntVar()
Guido van Rossum18468821994-06-20 07:49:28 +000037 buttons = []
38 i = 0
39 for but in args:
Tim Peters182b5ac2004-07-18 06:16:08 +000040 b = Button(bot, text=but, command=lambda v=var,i=i: v.set(i))
41 buttons.append(b)
42 if i == default:
43 bd = Frame(bot, relief=SUNKEN, borderwidth=1)
44 bd.pack(side=LEFT, expand=1, padx='3m', pady='2m')
45 b.lift()
46 b.pack (in_=bd, side=LEFT,
47 padx='2m', pady='2m', ipadx='2m', ipady='1m')
48 else:
49 b.pack (side=LEFT, expand=1,
50 padx='3m', pady='3m', ipadx='2m', ipady='1m')
51 i = i+1
Guido van Rossum18468821994-06-20 07:49:28 +000052
53 # 4. Set up a binding for <Return>, if there's a default,
54 # set a grab, and claim the focus too.
55
56 if default >= 0:
Tim Peters182b5ac2004-07-18 06:16:08 +000057 w.bind('<Return>',
58 lambda e, b=buttons[default], v=var, i=default:
59 (b.flash(),
60 v.set(i)))
Guido van Rossum18468821994-06-20 07:49:28 +000061
Guido van Rossum89cb67b1996-07-30 18:57:18 +000062 oldFocus = w.focus_get()
Guido van Rossum3c1d0871994-06-20 08:11:36 +000063 w.grab_set()
Guido van Rossum89cb67b1996-07-30 18:57:18 +000064 w.focus_set()
Guido van Rossum18468821994-06-20 07:49:28 +000065
66 # 5. Wait for the user to respond, then restore the focus
67 # and return the index of the selected button.
68
Guido van Rossum89cb67b1996-07-30 18:57:18 +000069 w.waitvar(var)
Guido van Rossum3c1d0871994-06-20 08:11:36 +000070 w.destroy()
Guido van Rossum89cb67b1996-07-30 18:57:18 +000071 if oldFocus: oldFocus.focus_set()
72 return var.get()
Guido van Rossum18468821994-06-20 07:49:28 +000073
74# The rest is the test program.
75
76def go():
77 i = dialog(mainWidget,
Tim Peters182b5ac2004-07-18 06:16:08 +000078 'Not Responding',
79 "The file server isn't responding right now; "
80 "I'll keep trying.",
81 '',
82 -1,
83 'OK')
Collin Winter6f2df4d2007-07-17 20:59:35 +000084 print('pressed button', i)
Guido van Rossum18468821994-06-20 07:49:28 +000085 i = dialog(mainWidget,
Tim Peters182b5ac2004-07-18 06:16:08 +000086 'File Modified',
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?',
90 'warning',
91 0,
92 'Save File',
93 'Discard Changes',
94 'Return To Editor')
Collin Winter6f2df4d2007-07-17 20:59:35 +000095 print('pressed button', i)
Guido van Rossum18468821994-06-20 07:49:28 +000096
97def test():
98 import sys
99 global mainWidget
100 mainWidget = Frame()
101 Pack.config(mainWidget)
Guido van Rossum89cb67b1996-07-30 18:57:18 +0000102 start = Button(mainWidget, text='Press Here To Start', command=go)
Guido van Rossum18468821994-06-20 07:49:28 +0000103 start.pack()
Guido van Rossum89cb67b1996-07-30 18:57:18 +0000104 endit = Button(mainWidget, text="Exit", command=sys.exit)
105 endit.pack(fill=BOTH)
Guido van Rossum3c1d0871994-06-20 08:11:36 +0000106 mainWidget.mainloop()
Guido van Rossum18468821994-06-20 07:49:28 +0000107
108if __name__ == '__main__':
109 test()