/Demo/tix/samples/Control.py

http://unladen-swallow.googlecode.com/ · Python · 122 lines · 68 code · 23 blank · 31 comment · 6 complexity · 9bc1d432380aa7646766354d4de69f45 MD5 · raw file

  1. # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: Control.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 tixControl widget -- it is an
  13. # entry widget with up/down arrow buttons. You can use the arrow buttons
  14. # to adjust the value inside the entry widget.
  15. #
  16. # This example program uses three Control widgets. One lets you select
  17. # integer values; one lets you select floating point values and the last
  18. # one lets you select a few names.
  19. import Tix
  20. TCL_ALL_EVENTS = 0
  21. def RunSample (root):
  22. control = DemoControl(root)
  23. control.mainloop()
  24. control.destroy()
  25. class DemoControl:
  26. def __init__(self, w):
  27. self.root = w
  28. self.exit = -1
  29. global demo_maker, demo_thrust, demo_num_engines
  30. demo_maker = Tix.StringVar()
  31. demo_thrust = Tix.DoubleVar()
  32. demo_num_engines = Tix.IntVar()
  33. demo_maker.set('P&W')
  34. demo_thrust.set(20000.0)
  35. demo_num_engines.set(2)
  36. top = Tix.Frame(w, bd=1, relief=Tix.RAISED)
  37. # $w.top.a allows only integer values
  38. #
  39. # [Hint] The -options switch sets the options of the subwidgets.
  40. # [Hint] We set the label.width subwidget option of the Controls to
  41. # be 16 so that their labels appear to be aligned.
  42. #
  43. a = Tix.Control(top, label='Number of Engines: ', integer=1,
  44. variable=demo_num_engines, min=1, max=4,
  45. options='entry.width 10 label.width 20 label.anchor e')
  46. b = Tix.Control(top, label='Thrust: ', integer=0,
  47. min='10000.0', max='60000.0', step=500,
  48. variable=demo_thrust,
  49. options='entry.width 10 label.width 20 label.anchor e')
  50. c = Tix.Control(top, label='Engine Maker: ', value='P&W',
  51. variable=demo_maker,
  52. options='entry.width 10 label.width 20 label.anchor e')
  53. # We can't define these in the init because the widget 'c' doesn't
  54. # exist yet and we need to reference it
  55. c['incrcmd'] = lambda w=c: adjust_maker(w, 1)
  56. c['decrcmd'] = lambda w=c: adjust_maker(w, -1)
  57. c['validatecmd'] = lambda w=c: validate_maker(w)
  58. a.pack(side=Tix.TOP, anchor=Tix.W)
  59. b.pack(side=Tix.TOP, anchor=Tix.W)
  60. c.pack(side=Tix.TOP, anchor=Tix.W)
  61. box = Tix.ButtonBox(w, orientation=Tix.HORIZONTAL)
  62. box.add('ok', text='Ok', underline=0, width=6,
  63. command=self.okcmd)
  64. box.add('cancel', text='Cancel', underline=0, width=6,
  65. command=self.quitcmd)
  66. box.pack(side=Tix.BOTTOM, fill=Tix.X)
  67. top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1)
  68. def okcmd (self):
  69. # tixDemo:Status "Selected %d of %s engines each of thrust %d", (demo_num_engines.get(), demo_maker.get(), demo_thrust.get())
  70. self.quitcmd()
  71. def quitcmd (self):
  72. self.exit = 0
  73. def mainloop(self):
  74. while self.exit < 0:
  75. self.root.tk.dooneevent(TCL_ALL_EVENTS)
  76. def destroy (self):
  77. self.root.destroy()
  78. maker_list = ['P&W', 'GE', 'Rolls Royce']
  79. def adjust_maker(w, inc):
  80. i = maker_list.index(demo_maker.get())
  81. i = i + inc
  82. if i >= len(maker_list):
  83. i = 0
  84. elif i < 0:
  85. i = len(maker_list) - 1
  86. # In Tcl/Tix we should return the string maker_list[i]. We can't
  87. # do that in Tkinter so we set the global variable. (This works).
  88. demo_maker.set(maker_list[i])
  89. def validate_maker(w):
  90. try:
  91. i = maker_list.index(demo_maker.get())
  92. except ValueError:
  93. # Works here though. Why ? Beats me.
  94. return maker_list[0]
  95. # Works here though. Why ? Beats me.
  96. return maker_list[i]
  97. if __name__ == '__main__':
  98. root = Tix.Tk()
  99. RunSample(root)