blob: eae9c7e7e96a980308145e6d3e833164f6b79813 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
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
Benjamin Petersond6d63f52009-01-04 18:53:28 +00007from 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
Georg Brandl856023a2010-10-25 17:50:20 +000027 msg = Message(top, width='3i', text=text)
Guido van Rossum89cb67b1996-07-30 18:57:18 +000028 msg.pack(side=RIGHT, expand=1, fill=BOTH, padx='3m', pady='3m')
Guido van Rossum18468821994-06-20 07:49:28 +000029 if bitmap:
Tim Peters182b5ac2004-07-18 06:16:08 +000030 bm = Label(top, bitmap=bitmap)
31 bm.pack(side=LEFT, padx='3m', pady='3m')
Guido van Rossum18468821994-06-20 07:49:28 +000032
33 # 3. Create a row of buttons at the bottom of the dialog.
34
Guido van Rossum89cb67b1996-07-30 18:57:18 +000035 var = IntVar()
Guido van Rossum18468821994-06-20 07:49:28 +000036 buttons = []
37 i = 0
38 for but in args:
Tim Peters182b5ac2004-07-18 06:16:08 +000039 b = Button(bot, text=but, command=lambda v=var,i=i: v.set(i))
40 buttons.append(b)
41 if i == default:
42 bd = Frame(bot, relief=SUNKEN, borderwidth=1)
43 bd.pack(side=LEFT, expand=1, padx='3m', pady='2m')
44 b.lift()
45 b.pack (in_=bd, side=LEFT,
46 padx='2m', pady='2m', ipadx='2m', ipady='1m')
47 else:
48 b.pack (side=LEFT, expand=1,
49 padx='3m', pady='3m', ipadx='2m', ipady='1m')
50 i = i+1
Guido van Rossum18468821994-06-20 07:49:28 +000051
52 # 4. Set up a binding for <Return>, if there's a default,
53 # set a grab, and claim the focus too.
54
55 if default >= 0:
Tim Peters182b5ac2004-07-18 06:16:08 +000056 w.bind('<Return>',
57 lambda e, b=buttons[default], v=var, i=default:
58 (b.flash(),
59 v.set(i)))
Guido van Rossum18468821994-06-20 07:49:28 +000060
Guido van Rossum89cb67b1996-07-30 18:57:18 +000061 oldFocus = w.focus_get()
Guido van Rossum3c1d0871994-06-20 08:11:36 +000062 w.grab_set()
Guido van Rossum89cb67b1996-07-30 18:57:18 +000063 w.focus_set()
Guido van Rossum18468821994-06-20 07:49:28 +000064
65 # 5. Wait for the user to respond, then restore the focus
66 # and return the index of the selected button.
67
Guido van Rossum89cb67b1996-07-30 18:57:18 +000068 w.waitvar(var)
Guido van Rossum3c1d0871994-06-20 08:11:36 +000069 w.destroy()
Guido van Rossum89cb67b1996-07-30 18:57:18 +000070 if oldFocus: oldFocus.focus_set()
71 return var.get()
Guido van Rossum18468821994-06-20 07:49:28 +000072
73# The rest is the test program.
74
75def go():
76 i = dialog(mainWidget,
Tim Peters182b5ac2004-07-18 06:16:08 +000077 'Not Responding',
78 "The file server isn't responding right now; "
79 "I'll keep trying.",
80 '',
81 -1,
82 'OK')
Collin Winter6f2df4d2007-07-17 20:59:35 +000083 print('pressed button', i)
Guido van Rossum18468821994-06-20 07:49:28 +000084 i = dialog(mainWidget,
Tim Peters182b5ac2004-07-18 06:16:08 +000085 'File Modified',
86 'File "tcl.h" has been modified since '
87 'the last time it was saved. '
88 'Do you want to save it before exiting the application?',
89 'warning',
90 0,
91 'Save File',
92 'Discard Changes',
93 'Return To Editor')
Collin Winter6f2df4d2007-07-17 20:59:35 +000094 print('pressed button', i)
Guido van Rossum18468821994-06-20 07:49:28 +000095
96def test():
97 import sys
98 global mainWidget
99 mainWidget = Frame()
100 Pack.config(mainWidget)
Guido van Rossum89cb67b1996-07-30 18:57:18 +0000101 start = Button(mainWidget, text='Press Here To Start', command=go)
Guido van Rossum18468821994-06-20 07:49:28 +0000102 start.pack()
Guido van Rossum89cb67b1996-07-30 18:57:18 +0000103 endit = Button(mainWidget, text="Exit", command=sys.exit)
104 endit.pack(fill=BOTH)
Guido van Rossum3c1d0871994-06-20 08:11:36 +0000105 mainWidget.mainloop()
Guido van Rossum18468821994-06-20 07:49:28 +0000106
107if __name__ == '__main__':
108 test()