Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 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 | |
| 7 | from Tkinter import * |
| 8 | |
| 9 | mainWidget = None |
| 10 | |
| 11 | def 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 | |
| 16 | w = Toplevel(master, {'class': 'Dialog'}) |
| 17 | w.title(title) |
| 18 | w.iconname('Dialog') |
| 19 | |
| 20 | top = Frame(w, {'relief': 'raised', 'bd': 1, |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 21 | Pack: {'side': 'top', 'fill': 'both'}}) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 22 | bot = Frame(w, {'relief': 'raised', 'bd': 1, |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 23 | Pack: {'side': 'bottom', 'fill': 'both'}}) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 24 | |
| 25 | # 2. Fill the top part with the bitmap and message. |
| 26 | |
| 27 | msg = Message(top, |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 28 | {'width': '3i', |
| 29 | 'text': text, |
| 30 | 'font': '-Adobe-Times-Medium-R-Normal-*-180-*', |
| 31 | Pack: {'side': 'right', 'expand': 1, |
| 32 | 'fill': 'both', |
| 33 | 'padx': '3m', 'pady': '3m'}}) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 34 | if bitmap: |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 35 | bm = Label(top, {'bitmap': bitmap, |
| 36 | Pack: {'side': 'left', |
| 37 | 'padx': '3m', 'pady': '3m'}}) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 38 | |
| 39 | # 3. Create a row of buttons at the bottom of the dialog. |
| 40 | |
| 41 | buttons = [] |
| 42 | i = 0 |
| 43 | for but in args: |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 44 | b = Button(bot, {'text': but, |
| 45 | 'command': ('set', 'button', i)}) |
| 46 | buttons.append(b) |
| 47 | if i == default: |
| 48 | bd = Frame(bot, {'relief': 'sunken', 'bd': 1, |
| 49 | Pack: {'side': 'left', 'expand': 1, |
| 50 | 'padx': '3m', 'pady': '2m'}}) |
| 51 | b.lift() |
| 52 | b.pack ({'in': bd, 'side': 'left', |
| 53 | 'padx': '2m', 'pady': '2m', |
| 54 | 'ipadx': '2m', 'ipady': '1m'}) |
| 55 | else: |
| 56 | b.pack ({'side': 'left', 'expand': 1, |
| 57 | 'padx': '3m', 'pady': '3m', |
| 58 | 'ipady': '2m', 'ipady': '1m'}) |
| 59 | i = i+1 |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 60 | |
| 61 | # 4. Set up a binding for <Return>, if there's a default, |
| 62 | # set a grab, and claim the focus too. |
| 63 | |
| 64 | if default >= 0: |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 65 | w.bind('<Return>', |
| 66 | lambda e, b=buttons[default], i=default: |
| 67 | (b.flash(), |
| 68 | b.setvar('button', i))) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 69 | |
| 70 | oldFocus = w.tk.call('focus') # XXX |
| 71 | w.grab_set() |
| 72 | w.focus() |
| 73 | |
| 74 | # 5. Wait for the user to respond, then restore the focus |
| 75 | # and return the index of the selected button. |
| 76 | |
| 77 | w.waitvar('button') |
| 78 | w.destroy() |
| 79 | w.tk.call('focus', oldFocus) # XXX |
| 80 | return w.getint(w.getvar('button')) |
| 81 | |
| 82 | def strdialog(master, title, text, bitmap, default, *args): |
| 83 | |
| 84 | # 1. Create the top-level window and divide it into top |
| 85 | # and bottom parts. |
| 86 | |
| 87 | w = Toplevel(master, {'class': 'Dialog'}) |
| 88 | w.title(title) |
| 89 | w.iconname('Dialog') |
| 90 | |
| 91 | top = Frame(w, {'relief': 'raised', 'bd': 1, |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 92 | Pack: {'side': 'top', 'fill': 'both'}}) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 93 | if args: |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 94 | bot = Frame(w, {'relief': 'raised', 'bd': 1, |
| 95 | Pack: {'side': 'bottom', 'fill': 'both'}}) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 96 | |
| 97 | # 2. Fill the top part with the bitmap, message and input field. |
| 98 | |
| 99 | if bitmap: |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 100 | bm = Label(top, {'bitmap': bitmap, |
| 101 | Pack: {'side': 'left', |
| 102 | 'padx': '3m', 'pady': '3m'}}) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 103 | |
| 104 | msg = Message(top, |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 105 | {'width': '3i', |
| 106 | 'text': text, |
| 107 | 'font': '-Adobe-Times-Medium-R-Normal-*-180-*', |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 108 | Pack: {'side': 'left', |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 109 | 'fill': 'both', |
| 110 | 'padx': '3m', 'pady': '3m'}}) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 111 | |
| 112 | field = Entry(top, |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 113 | {'relief':'sunken', |
| 114 | Pack:{'side':'left', |
| 115 | 'fill':'x', |
| 116 | 'expand':1, |
| 117 | 'padx':'3m', 'pady':'3m'}}) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 118 | # 3. Create a row of buttons at the bottom of the dialog. |
| 119 | |
| 120 | buttons = [] |
| 121 | i = 0 |
| 122 | for but in args: |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 123 | b = Button(bot, {'text': but, |
| 124 | 'command': ('set', 'button', i)}) |
| 125 | buttons.append(b) |
| 126 | if i == default: |
| 127 | bd = Frame(bot, {'relief': 'sunken', 'bd': 1, |
| 128 | Pack: {'side': 'left', 'expand': 1, |
| 129 | 'padx': '3m', 'pady': '2m'}}) |
| 130 | b.lift() |
| 131 | b.pack ({'in': bd, 'side': 'left', |
| 132 | 'padx': '2m', 'pady': '2m', |
| 133 | 'ipadx': '2m', 'ipady': '1m'}) |
| 134 | else: |
| 135 | b.pack ({'side': 'left', 'expand': 1, |
| 136 | 'padx': '3m', 'pady': '3m', |
| 137 | 'ipady': '2m', 'ipady': '1m'}) |
| 138 | i = i+1 |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 139 | |
| 140 | # 4. Set up a binding for <Return>, if there's a default, |
| 141 | # set a grab, and claim the focus too. |
| 142 | |
| 143 | if not args: |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 144 | w.bind('<Return>', lambda arg, top=top: top.setvar('button', 0)) |
| 145 | field.bind('<Return>', lambda arg, top=top: top.setvar('button', 0)) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 146 | elif default >= 0: |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 147 | w.bind('<Return>', |
| 148 | lambda e, b=buttons[default], i=default: |
| 149 | (b.flash(), |
| 150 | b.setvar('button', i))) |
| 151 | field.bind('<Return>', |
| 152 | lambda e, b=buttons[default], i=default: |
| 153 | (b.flash(), |
| 154 | b.setvar('button', i))) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 155 | |
| 156 | oldFocus = w.tk.call('focus') # XXX |
| 157 | w.grab_set() |
| 158 | field.focus() |
| 159 | |
| 160 | # 5. Wait for the user to respond, then restore the focus |
| 161 | # and return the index of the selected button. |
| 162 | |
| 163 | w.waitvar('button') |
| 164 | v = field.get() |
| 165 | w.destroy() |
| 166 | w.tk.call('focus', oldFocus) # XXX |
| 167 | if args: |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 168 | return v, w.getint(w.getvar('button')) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 169 | else: |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 170 | return v |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 171 | |
| 172 | def message(str): |
| 173 | i = dialog(mainWidget, 'Message', str, '', 0, 'OK') |
| 174 | |
| 175 | def askyn(str): |
| 176 | i = dialog(mainWidget, 'Question', str, '', 0, 'No', 'Yes') |
| 177 | return i |
| 178 | |
| 179 | def askync(str): |
| 180 | i = dialog(mainWidget, 'Question', str, '', 0, 'Cancel', 'No', 'Yes') |
| 181 | return i-1 |
| 182 | |
| 183 | def askstr(str): |
| 184 | i = strdialog(mainWidget, 'Question', str, '', 0) |
| 185 | return i |
| 186 | |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 187 | def askfile(str): # XXXX For now... |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 188 | i = strdialog(mainWidget, 'Question', str, '', 0) |
| 189 | return i |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 191 | # The rest is the test program. |
| 192 | |
| 193 | def _go(): |
| 194 | i = dialog(mainWidget, |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 195 | 'Not Responding', |
| 196 | "The file server isn't responding right now; " |
| 197 | "I'll keep trying.", |
| 198 | '', |
| 199 | -1, |
| 200 | 'OK') |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 201 | print('pressed button', i) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 202 | i = dialog(mainWidget, |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 203 | 'File Modified', |
| 204 | 'File "tcl.h" has been modified since ' |
| 205 | 'the last time it was saved. ' |
| 206 | 'Do you want to save it before exiting the application?', |
| 207 | 'warning', |
| 208 | 0, |
| 209 | 'Save File', |
| 210 | 'Discard Changes', |
| 211 | 'Return To Editor') |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 212 | print('pressed button', i) |
| 213 | print(message('Test of message')) |
| 214 | print(askyn('Test of yes/no')) |
| 215 | print(askync('Test of yes/no/cancel')) |
| 216 | print(askstr('Type a string:')) |
| 217 | print(strdialog(mainWidget, 'Question', 'Another string:', '', |
| 218 | 0, 'Save', 'Save as text')) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 219 | |
| 220 | def _test(): |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 221 | global mainWidget |
| 222 | mainWidget = Frame() |
| 223 | Pack.config(mainWidget) |
| 224 | start = Button(mainWidget, |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 225 | {'text': 'Press Here To Start', 'command': _go}) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 226 | start.pack() |
| 227 | endit = Button(mainWidget, |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 228 | {'text': 'Exit', |
| 229 | 'command': 'exit', |
| 230 | Pack: {'fill' : 'both'}}) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 231 | mainWidget.mainloop() |
| 232 | |
| 233 | if __name__ == '__main__': |
| 234 | _test() |