Updated for 2.1b2 distribution.
[python/dscho.git] / Demo / tix / samples / Control.py
blobb8e1156582d362c3055f051dd86f16048c180802
1 #!/usr/local/bin/python
2 #
3 # $Id$
5 # Tix Demostration Program
7 # This sample program is structured in such a way so that it can be
8 # executed from the Tix demo program "tixwidgets": 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.
13 # This file demonstrates the use of the tixControl widget -- it is an
14 # entry widget with up/down arrow buttons. You can use the arrow buttons
15 # to adjust the value inside the entry widget.
17 # This example program uses three Control widgets. One lets you select
18 # integer values; one lets you select floating point values and the last
19 # one lets you select a few names.
21 import Tix
23 def RunSample(w):
24 global demo_maker, demo_thrust, demo_num_engines
26 demo_maker = Tix.StringVar()
27 demo_thrust = Tix.DoubleVar()
28 demo_num_engines = Tix.IntVar()
29 demo_maker.set('P&W')
30 demo_thrust.set(20000.0)
31 demo_num_engines.set(2)
33 top = Tix.Frame(w, bd=1, relief=Tix.RAISED)
35 # $w.top.a allows only integer values
37 # [Hint] The -options switch sets the options of the subwidgets.
38 # [Hint] We set the label.width subwidget option of the Controls to
39 # be 16 so that their labels appear to be aligned.
41 a = Tix.Control(top, label='Number of Engines: ', integer=1,
42 variable=demo_num_engines, min=1, max=4,
43 options='entry.width 10 label.width 20 label.anchor e')
45 b = Tix.Control(top, label='Thrust: ', integer=0,
46 min='10000.0', max='60000.0', step=500,
47 variable=demo_thrust,
48 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')
54 # We can't define these in the init because the widget 'c' doesn't
55 # exist yet and we need to reference it
56 c['incrcmd'] = lambda w=c: adjust_maker(w, 1)
57 c['decrcmd'] = lambda w=c: adjust_maker(w, -1)
58 c['validatecmd'] = lambda w=c: validate_maker(w)
60 a.pack(side=Tix.TOP, anchor=Tix.W)
61 b.pack(side=Tix.TOP, anchor=Tix.W)
62 c.pack(side=Tix.TOP, anchor=Tix.W)
64 box = Tix.ButtonBox(w, orientation=Tix.HORIZONTAL)
65 box.add('ok', text='Ok', underline=0, width=6,
66 command=lambda w=w: ok_command(w))
67 box.add('cancel', text='Cancel', underline=0, width=6,
68 command=lambda w=w: w.destroy())
69 box.pack(side=Tix.BOTTOM, fill=Tix.X)
70 top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1)
72 maker_list = ['P&W', 'GE', 'Rolls Royce']
74 def adjust_maker(w, inc):
75 i = maker_list.index(demo_maker.get())
76 i = i + inc
77 if i >= len(maker_list):
78 i = 0
79 elif i < 0:
80 i = len(maker_list) - 1
82 # In Tcl/Tix we should return the string maker_list[i]. We can't
83 # do that in Tkinter so we set the global variable. (This works).
84 demo_maker.set(maker_list[i])
86 def validate_maker(w):
87 try:
88 i = maker_list.index(demo_maker.get())
89 except:
90 # Works here though. Why ? Beats me.
91 return maker_list[0]
92 # Works here though. Why ? Beats me.
93 return maker_list[i]
95 def ok_command(w):
96 print "Selected", demo_num_engines.get(), "of", demo_maker.get(), " engines each of thrust", demo_thrust.get()
97 w.destroy()
99 if __name__ == '__main__':
100 root = Tix.Tk()
101 RunSample(root)
102 root.mainloop()