#! /usr/bin/python2.7 # A Python function that generates dialog boxes with a text message, # optional bitmap, and any number of buttons. # Cf. Ousterhout, Tcl and the Tk Toolkit, Figs. 27.2-3, pp. 269-270. from Tkinter import * import sys def dialog(master, title, text, bitmap, default, *args): # 1. Create the top-level window and divide it into top # and bottom parts. w = Toplevel(master, class_='Dialog') w.title(title) w.iconname('Dialog') top = Frame(w, relief=RAISED, borderwidth=1) top.pack(side=TOP, fill=BOTH) bot = Frame(w, relief=RAISED, borderwidth=1) bot.pack(side=BOTTOM, fill=BOTH) # 2. Fill the top part with the bitmap and message. msg = Message(top, width='3i', text=text, font='-Adobe-Times-Medium-R-Normal-*-180-*') msg.pack(side=RIGHT, expand=1, fill=BOTH, padx='3m', pady='3m') if bitmap: bm = Label(top, bitmap=bitmap) bm.pack(side=LEFT, padx='3m', pady='3m') # 3. Create a row of buttons at the bottom of the dialog. var = IntVar() buttons = [] i = 0 for but in args: b = Button(bot, text=but, command=lambda v=var,i=i: v.set(i)) buttons.append(b) if i == default: bd = Frame(bot, relief=SUNKEN, borderwidth=1) bd.pack(side=LEFT, expand=1, padx='3m', pady='2m') b.lift() b.pack (in_=bd, side=LEFT, padx='2m', pady='2m', ipadx='2m', ipady='1m') else: b.pack (side=LEFT, expand=1, padx='3m', pady='3m', ipadx='2m', ipady='1m') i = i+1 # 4. Set up a binding for <Return>, if there's a default, # set a grab, and claim the focus too. if default >= 0: w.bind('<Return>', lambda e, b=buttons[default], v=var, i=default: (b.flash(), v.set(i))) oldFocus = w.focus_get() w.grab_set() w.focus_set() # 5. Wait for the user to respond, then restore the focus # and return the index of the selected button. w.waitvar(var) w.destroy() if oldFocus: oldFocus.focus_set() return var.get() # The rest is the test program. def go(): i = dialog(mainWidget, 'Not Responding', "The file server isn't responding right now; " "I'll keep trying.", '', -1, 'OK') print 'pressed button', i i = dialog(mainWidget, 'File Modified', 'File "tcl.h" has been modified since ' 'the last time it was saved. ' 'Do you want to save it before exiting the application?', 'warning', 0, 'Save File', 'Discard Changes', 'Return To Editor') print 'pressed button', i def test(): import sys global mainWidget mainWidget = Frame() Pack.config(mainWidget) start = Button(mainWidget, text='Press Here To Start', command=go) start.pack() endit = Button(mainWidget, text="Exit", command=sys.exit) endit.pack(fill=BOTH) mainWidget.mainloop() if __name__ == '__main__': test()
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
AttrDialog.py | File | 14 KB | 0644 |
|
AttrDialog.pyc | File | 18.33 KB | 0644 |
|
AttrDialog.pyo | File | 18.33 KB | 0644 |
|
ManPage.py | File | 6.7 KB | 0644 |
|
ManPage.pyc | File | 6.27 KB | 0644 |
|
ManPage.pyo | File | 6.27 KB | 0644 |
|
MimeViewer.py | File | 4.39 KB | 0755 |
|
MimeViewer.pyc | File | 4.83 KB | 0644 |
|
MimeViewer.pyo | File | 4.83 KB | 0644 |
|
ShellWindow.py | File | 4.07 KB | 0644 |
|
ShellWindow.pyc | File | 5.47 KB | 0644 |
|
ShellWindow.pyo | File | 5.47 KB | 0644 |
|
brownian.py | File | 1.07 KB | 0644 |
|
brownian.pyc | File | 1.59 KB | 0644 |
|
brownian.pyo | File | 1.59 KB | 0644 |
|
brownian2.py | File | 1.35 KB | 0644 |
|
brownian2.pyc | File | 1.74 KB | 0644 |
|
brownian2.pyo | File | 1.74 KB | 0644 |
|
canvasevents.py | File | 6.45 KB | 0755 |
|
canvasevents.pyc | File | 10.46 KB | 0644 |
|
canvasevents.pyo | File | 10.46 KB | 0644 |
|
dialog.py | File | 3.13 KB | 0755 |
|
dialog.pyc | File | 3.11 KB | 0644 |
|
dialog.pyo | File | 3.11 KB | 0644 |
|
electrons.py | File | 2.27 KB | 0755 |
|
electrons.pyc | File | 2.33 KB | 0644 |
|
electrons.pyo | File | 2.33 KB | 0644 |
|
hanoi.py | File | 4.54 KB | 0644 |
|
hanoi.pyc | File | 3.82 KB | 0644 |
|
hanoi.pyo | File | 3.82 KB | 0644 |
|
hello.py | File | 331 B | 0644 |
|
hello.pyc | File | 701 B | 0644 |
|
hello.pyo | File | 701 B | 0644 |
|
imagedraw.py | File | 509 B | 0644 |
|
imagedraw.pyc | File | 1.13 KB | 0644 |
|
imagedraw.pyo | File | 1.13 KB | 0644 |
|
imageview.py | File | 205 B | 0644 |
|
imageview.pyc | File | 570 B | 0644 |
|
imageview.pyo | File | 570 B | 0644 |
|
kill.py | File | 3.79 KB | 0755 |
|
kill.pyc | File | 4.81 KB | 0644 |
|
kill.pyo | File | 4.81 KB | 0644 |
|
listtree.py | File | 991 B | 0644 |
|
listtree.pyc | File | 1.5 KB | 0644 |
|
listtree.pyo | File | 1.5 KB | 0644 |
|
mbox.py | File | 7.3 KB | 0755 |
|
mbox.pyc | File | 8.17 KB | 0644 |
|
mbox.pyo | File | 8.17 KB | 0644 |
|
newmenubardemo.py | File | 1.2 KB | 0755 |
|
newmenubardemo.pyc | File | 1.59 KB | 0644 |
|
newmenubardemo.pyo | File | 1.59 KB | 0644 |
|
optionmenu.py | File | 441 B | 0644 |
|
optionmenu.pyc | File | 594 B | 0644 |
|
optionmenu.pyo | File | 594 B | 0644 |
|
paint.py | File | 1.85 KB | 0644 |
|
paint.pyc | File | 2.19 KB | 0644 |
|
paint.pyo | File | 2.19 KB | 0644 |
|
rmt.py | File | 4.39 KB | 0755 |
|
rmt.pyc | File | 4.93 KB | 0644 |
|
rmt.pyo | File | 4.93 KB | 0644 |
|
solitaire.py | File | 17.37 KB | 0755 |
|
solitaire.pyc | File | 20.41 KB | 0644 |
|
solitaire.pyo | File | 20.41 KB | 0644 |
|
sortvisu.py | File | 18.89 KB | 0755 |
|
sortvisu.pyc | File | 23.18 KB | 0644 |
|
sortvisu.pyo | File | 23.18 KB | 0644 |
|
ss1.py | File | 25.57 KB | 0644 |
|
ss1.pyc | File | 31.67 KB | 0644 |
|
ss1.pyo | File | 31.07 KB | 0644 |
|
svkill.py | File | 4.75 KB | 0755 |
|
svkill.pyc | File | 5.8 KB | 0644 |
|
svkill.pyo | File | 5.8 KB | 0644 |
|
switch.py | File | 1.52 KB | 0644 |
|
switch.pyc | File | 2.85 KB | 0644 |
|
switch.pyo | File | 2.85 KB | 0644 |
|
tkman.py | File | 8.79 KB | 0755 |
|
tkman.pyc | File | 8.88 KB | 0644 |
|
tkman.pyo | File | 8.88 KB | 0644 |
|
wish.py | File | 623 B | 0644 |
|
wish.pyc | File | 737 B | 0644 |
|
wish.pyo | File | 737 B | 0644 |
|