/Demo/tkinter/guido/dialog.py

http://unladen-swallow.googlecode.com/ · Python · 109 lines · 76 code · 20 blank · 13 comment · 7 complexity · ae7f1d5009c0ca5eab3280ea97538885 MD5 · raw file

  1. #! /usr/bin/env python
  2. # A Python function that generates dialog boxes with a text message,
  3. # optional bitmap, and any number of buttons.
  4. # Cf. Ousterhout, Tcl and the Tk Toolkit, Figs. 27.2-3, pp. 269-270.
  5. from Tkinter import *
  6. import sys
  7. def dialog(master, title, text, bitmap, default, *args):
  8. # 1. Create the top-level window and divide it into top
  9. # and bottom parts.
  10. w = Toplevel(master, class_='Dialog')
  11. w.title(title)
  12. w.iconname('Dialog')
  13. top = Frame(w, relief=RAISED, borderwidth=1)
  14. top.pack(side=TOP, fill=BOTH)
  15. bot = Frame(w, relief=RAISED, borderwidth=1)
  16. bot.pack(side=BOTTOM, fill=BOTH)
  17. # 2. Fill the top part with the bitmap and message.
  18. msg = Message(top, width='3i', text=text,
  19. font='-Adobe-Times-Medium-R-Normal-*-180-*')
  20. msg.pack(side=RIGHT, expand=1, fill=BOTH, padx='3m', pady='3m')
  21. if bitmap:
  22. bm = Label(top, bitmap=bitmap)
  23. bm.pack(side=LEFT, padx='3m', pady='3m')
  24. # 3. Create a row of buttons at the bottom of the dialog.
  25. var = IntVar()
  26. buttons = []
  27. i = 0
  28. for but in args:
  29. b = Button(bot, text=but, command=lambda v=var,i=i: v.set(i))
  30. buttons.append(b)
  31. if i == default:
  32. bd = Frame(bot, relief=SUNKEN, borderwidth=1)
  33. bd.pack(side=LEFT, expand=1, padx='3m', pady='2m')
  34. b.lift()
  35. b.pack (in_=bd, side=LEFT,
  36. padx='2m', pady='2m', ipadx='2m', ipady='1m')
  37. else:
  38. b.pack (side=LEFT, expand=1,
  39. padx='3m', pady='3m', ipadx='2m', ipady='1m')
  40. i = i+1
  41. # 4. Set up a binding for <Return>, if there's a default,
  42. # set a grab, and claim the focus too.
  43. if default >= 0:
  44. w.bind('<Return>',
  45. lambda e, b=buttons[default], v=var, i=default:
  46. (b.flash(),
  47. v.set(i)))
  48. oldFocus = w.focus_get()
  49. w.grab_set()
  50. w.focus_set()
  51. # 5. Wait for the user to respond, then restore the focus
  52. # and return the index of the selected button.
  53. w.waitvar(var)
  54. w.destroy()
  55. if oldFocus: oldFocus.focus_set()
  56. return var.get()
  57. # The rest is the test program.
  58. def go():
  59. i = dialog(mainWidget,
  60. 'Not Responding',
  61. "The file server isn't responding right now; "
  62. "I'll keep trying.",
  63. '',
  64. -1,
  65. 'OK')
  66. print 'pressed button', i
  67. i = dialog(mainWidget,
  68. 'File Modified',
  69. 'File "tcl.h" has been modified since '
  70. 'the last time it was saved. '
  71. 'Do you want to save it before exiting the application?',
  72. 'warning',
  73. 0,
  74. 'Save File',
  75. 'Discard Changes',
  76. 'Return To Editor')
  77. print 'pressed button', i
  78. def test():
  79. import sys
  80. global mainWidget
  81. mainWidget = Frame()
  82. Pack.config(mainWidget)
  83. start = Button(mainWidget, text='Press Here To Start', command=go)
  84. start.pack()
  85. endit = Button(mainWidget, text="Exit", command=sys.exit)
  86. endit.pack(fill=BOTH)
  87. mainWidget.mainloop()
  88. if __name__ == '__main__':
  89. test()