blob: 3a344c1e27660bc8fb32eca37f4c0cbf35b15e79 [file] [log] [blame]
Martin v. Löwis8ec03e02002-03-17 18:19:13 +00001# -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
2#
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00003# $Id$
4#
5# Tix Demostration Program
6#
7# This sample program is structured in such a way so that it can be
Martin v. Löwis8ec03e02002-03-17 18:19:13 +00008# executed from the Tix demo program "tixwidgets.py": it must have a
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00009# 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
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.
16#
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.
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000020
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000021import Tix
22
Tim Peters182b5ac2004-07-18 06:16:08 +000023TCL_ALL_EVENTS = 0
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000024
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000025def RunSample (root):
26 control = DemoControl(root)
27 control.mainloop()
28 control.destroy()
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000029
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000030class DemoControl:
31 def __init__(self, w):
32 self.root = w
33 self.exit = -1
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000034
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000035 global demo_maker, demo_thrust, demo_num_engines
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000036
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000037 demo_maker = Tix.StringVar()
38 demo_thrust = Tix.DoubleVar()
39 demo_num_engines = Tix.IntVar()
40 demo_maker.set('P&W')
41 demo_thrust.set(20000.0)
42 demo_num_engines.set(2)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000043
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000044 top = Tix.Frame(w, bd=1, relief=Tix.RAISED)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000045
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000046 # $w.top.a allows only integer values
47 #
48 # [Hint] The -options switch sets the options of the subwidgets.
Tim Peters182b5ac2004-07-18 06:16:08 +000049 # [Hint] We set the label.width subwidget option of the Controls to
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000050 # be 16 so that their labels appear to be aligned.
51 #
52 a = Tix.Control(top, label='Number of Engines: ', integer=1,
53 variable=demo_num_engines, min=1, max=4,
54 options='entry.width 10 label.width 20 label.anchor e')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000055
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000056 b = Tix.Control(top, label='Thrust: ', integer=0,
57 min='10000.0', max='60000.0', step=500,
58 variable=demo_thrust,
59 options='entry.width 10 label.width 20 label.anchor e')
60
61 c = Tix.Control(top, label='Engine Maker: ', value='P&W',
62 variable=demo_maker,
63 options='entry.width 10 label.width 20 label.anchor e')
64
65 # We can't define these in the init because the widget 'c' doesn't
66 # exist yet and we need to reference it
67 c['incrcmd'] = lambda w=c: adjust_maker(w, 1)
68 c['decrcmd'] = lambda w=c: adjust_maker(w, -1)
69 c['validatecmd'] = lambda w=c: validate_maker(w)
70
71 a.pack(side=Tix.TOP, anchor=Tix.W)
72 b.pack(side=Tix.TOP, anchor=Tix.W)
73 c.pack(side=Tix.TOP, anchor=Tix.W)
74
75 box = Tix.ButtonBox(w, orientation=Tix.HORIZONTAL)
76 box.add('ok', text='Ok', underline=0, width=6,
77 command=self.okcmd)
78 box.add('cancel', text='Cancel', underline=0, width=6,
79 command=self.quitcmd)
80 box.pack(side=Tix.BOTTOM, fill=Tix.X)
81 top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1)
82
83 def okcmd (self):
84 # tixDemo:Status "Selected %d of %s engines each of thrust %d", (demo_num_engines.get(), demo_maker.get(), demo_thrust.get())
85 self.quitcmd()
Tim Peters182b5ac2004-07-18 06:16:08 +000086
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000087 def quitcmd (self):
88 self.exit = 0
89
90 def mainloop(self):
91 while self.exit < 0:
92 self.root.tk.dooneevent(TCL_ALL_EVENTS)
93
94 def destroy (self):
95 self.root.destroy()
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000096
97maker_list = ['P&W', 'GE', 'Rolls Royce']
98
99def adjust_maker(w, inc):
100 i = maker_list.index(demo_maker.get())
101 i = i + inc
102 if i >= len(maker_list):
Tim Peters182b5ac2004-07-18 06:16:08 +0000103 i = 0
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000104 elif i < 0:
Tim Peters182b5ac2004-07-18 06:16:08 +0000105 i = len(maker_list) - 1
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000106
107 # In Tcl/Tix we should return the string maker_list[i]. We can't
108 # do that in Tkinter so we set the global variable. (This works).
109 demo_maker.set(maker_list[i])
110
111def validate_maker(w):
112 try:
Tim Peters182b5ac2004-07-18 06:16:08 +0000113 i = maker_list.index(demo_maker.get())
Fred Drakeef4cdad2001-05-11 19:52:03 +0000114 except ValueError:
Tim Peters182b5ac2004-07-18 06:16:08 +0000115 # Works here though. Why ? Beats me.
116 return maker_list[0]
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000117 # Works here though. Why ? Beats me.
118 return maker_list[i]
119
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000120if __name__ == '__main__':
121 root = Tix.Tk()
122 RunSample(root)