/Demo/tix/samples/ComboBox.py

http://unladen-swallow.googlecode.com/ · Python · 102 lines · 50 code · 15 blank · 37 comment · 1 complexity · d27e9e37d37eea0b46b7f3d0566fbe27 MD5 · raw file

  1. # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: ComboBox.py 36560 2004-07-18 06:16:08Z tim_one $
  4. #
  5. # Tix Demostration Program
  6. #
  7. # This sample program is structured in such a way so that it can be
  8. # executed from the Tix demo program "tixwidgets.py": it must have a
  9. # procedure called "RunSample". It should also have the "if" statment
  10. # at the end of this file so that it can be run as a standalone
  11. # program.
  12. # This file demonstrates the use of the tixComboBox widget, which is close
  13. # to the MS Window Combo Box control.
  14. #
  15. import Tix
  16. def RunSample(w):
  17. global demo_month, demo_year
  18. top = Tix.Frame(w, bd=1, relief=Tix.RAISED)
  19. demo_month = Tix.StringVar()
  20. demo_year = Tix.StringVar()
  21. # $w.top.a is a drop-down combo box. It is not editable -- who wants
  22. # to invent new months?
  23. #
  24. # [Hint] The -options switch sets the options of the subwidgets.
  25. # [Hint] We set the label.width subwidget option of both comboboxes to
  26. # be 10 so that their labels appear to be aligned.
  27. #
  28. a = Tix.ComboBox(top, label="Month: ", dropdown=1,
  29. command=select_month, editable=0, variable=demo_month,
  30. options='listbox.height 6 label.width 10 label.anchor e')
  31. # $w.top.b is a non-drop-down combo box. It is not editable: we provide
  32. # four choices for the user, but he can enter an alternative year if he
  33. # wants to.
  34. #
  35. # [Hint] Use the padY and anchor options of the label subwidget to
  36. # align the label with the entry subwidget.
  37. # [Hint] Notice that you should use padY (the NAME of the option) and not
  38. # pady (the SWITCH of the option).
  39. #
  40. b = Tix.ComboBox(top, label="Year: ", dropdown=0,
  41. command=select_year, editable=1, variable=demo_year,
  42. options='listbox.height 4 label.padY 5 label.width 10 label.anchor ne')
  43. a.pack(side=Tix.TOP, anchor=Tix.W)
  44. b.pack(side=Tix.TOP, anchor=Tix.W)
  45. a.insert(Tix.END, 'January')
  46. a.insert(Tix.END, 'February')
  47. a.insert(Tix.END, 'March')
  48. a.insert(Tix.END, 'April')
  49. a.insert(Tix.END, 'May')
  50. a.insert(Tix.END, 'June')
  51. a.insert(Tix.END, 'July')
  52. a.insert(Tix.END, 'August')
  53. a.insert(Tix.END, 'September')
  54. a.insert(Tix.END, 'October')
  55. a.insert(Tix.END, 'November')
  56. a.insert(Tix.END, 'December')
  57. b.insert(Tix.END, '1992')
  58. b.insert(Tix.END, '1993')
  59. b.insert(Tix.END, '1994')
  60. b.insert(Tix.END, '1995')
  61. b.insert(Tix.END, '1996')
  62. # Use "tixSetSilent" to set the values of the combo box if you
  63. # don't want your -command procedures (cbx:select_month and
  64. # cbx:select_year) to be called.
  65. #
  66. a.set_silent('January')
  67. b.set_silent('1995')
  68. box = Tix.ButtonBox(w, orientation=Tix.HORIZONTAL)
  69. box.add('ok', text='Ok', underline=0, width=6,
  70. command=lambda w=w: ok_command(w))
  71. box.add('cancel', text='Cancel', underline=0, width=6,
  72. command=lambda w=w: w.destroy())
  73. box.pack(side=Tix.BOTTOM, fill=Tix.X)
  74. top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1)
  75. def select_month(event=None):
  76. # tixDemo:Status "Month = %s" % demo_month.get()
  77. pass
  78. def select_year(event=None):
  79. # tixDemo:Status "Year = %s" % demo_year.get()
  80. pass
  81. def ok_command(w):
  82. # tixDemo:Status "Month = %s, Year= %s" % (demo_month.get(), demo_year.get())
  83. w.destroy()
  84. if __name__ == '__main__':
  85. root = Tix.Tk()
  86. RunSample(root)
  87. root.mainloop()