annotate Demo/tix/samples/Control.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents c3a096bfbd6b
children c25e5b620a4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
1 # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
2 #
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
3 # $Id$
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
4 #
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
5 # Tix Demostration Program
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
6 #
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
7 # This sample program is structured in such a way so that it can be
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
8 # executed from the Tix demo program "tixwidgets.py": it must have a
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
9 # procedure called "RunSample". It should also have the "if" statment
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
10 # at the end of this file so that it can be run as a standalone
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
11 # program.
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
12
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
13 # This file demonstrates the use of the tixControl widget -- it is an
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
14 # entry widget with up/down arrow buttons. You can use the arrow buttons
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
15 # to adjust the value inside the entry widget.
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
16 #
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
17 # This example program uses three Control widgets. One lets you select
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
18 # integer values; one lets you select floating point values and the last
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
19 # one lets you select a few names.
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
20
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
21 import Tix
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
22
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 22345
diff changeset
23 TCL_ALL_EVENTS = 0
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
24
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
25 def RunSample (root):
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
26 control = DemoControl(root)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
27 control.mainloop()
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
28 control.destroy()
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
29
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
30 class DemoControl:
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
31 def __init__(self, w):
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
32 self.root = w
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
33 self.exit = -1
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
34
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
35 global demo_maker, demo_thrust, demo_num_engines
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
36
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
37 demo_maker = Tix.StringVar()
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
38 demo_thrust = Tix.DoubleVar()
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
39 demo_num_engines = Tix.IntVar()
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
40 demo_maker.set('P&W')
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
41 demo_thrust.set(20000.0)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
42 demo_num_engines.set(2)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
43
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
44 top = Tix.Frame(w, bd=1, relief=Tix.RAISED)
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
45
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
46 # $w.top.a allows only integer values
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
47 #
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
48 # [Hint] The -options switch sets the options of the subwidgets.
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 22345
diff changeset
49 # [Hint] We set the label.width subwidget option of the Controls to
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
50 # be 16 so that their labels appear to be aligned.
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
51 #
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
52 a = Tix.Control(top, label='Number of Engines: ', integer=1,
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
53 variable=demo_num_engines, min=1, max=4,
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
54 options='entry.width 10 label.width 20 label.anchor e')
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
55
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
56 b = Tix.Control(top, label='Thrust: ', integer=0,
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
57 min='10000.0', max='60000.0', step=500,
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
58 variable=demo_thrust,
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
59 options='entry.width 10 label.width 20 label.anchor e')
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
60
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
61 c = Tix.Control(top, label='Engine Maker: ', value='P&W',
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
62 variable=demo_maker,
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
63 options='entry.width 10 label.width 20 label.anchor e')
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
64
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
65 # We can't define these in the init because the widget 'c' doesn't
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
66 # exist yet and we need to reference it
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
67 c['incrcmd'] = lambda w=c: adjust_maker(w, 1)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
68 c['decrcmd'] = lambda w=c: adjust_maker(w, -1)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
69 c['validatecmd'] = lambda w=c: validate_maker(w)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
70
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
71 a.pack(side=Tix.TOP, anchor=Tix.W)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
72 b.pack(side=Tix.TOP, anchor=Tix.W)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
73 c.pack(side=Tix.TOP, anchor=Tix.W)
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
74
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
75 box = Tix.ButtonBox(w, orientation=Tix.HORIZONTAL)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
76 box.add('ok', text='Ok', underline=0, width=6,
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
77 command=self.okcmd)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
78 box.add('cancel', text='Cancel', underline=0, width=6,
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
79 command=self.quitcmd)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
80 box.pack(side=Tix.BOTTOM, fill=Tix.X)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
81 top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1)
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
82
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
83 def okcmd (self):
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
84 # tixDemo:Status "Selected %d of %s engines each of thrust %d", (demo_num_engines.get(), demo_maker.get(), demo_thrust.get())
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
85 self.quitcmd()
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 22345
diff changeset
86
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
87 def quitcmd (self):
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
88 self.exit = 0
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
89
22345
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
90 def mainloop(self):
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
91 while self.exit < 0:
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
92 self.root.tk.dooneevent(TCL_ALL_EVENTS)
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
93
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
94 def destroy (self):
1f73251c625a Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 17804
diff changeset
95 self.root.destroy()
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
96
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
97 maker_list = ['P&W', 'GE', 'Rolls Royce']
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
98
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
99 def adjust_maker(w, inc):
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
100 i = maker_list.index(demo_maker.get())
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
101 i = i + inc
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
102 if i >= len(maker_list):
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 22345
diff changeset
103 i = 0
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
104 elif i < 0:
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 22345
diff changeset
105 i = len(maker_list) - 1
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
106
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
107 # In Tcl/Tix we should return the string maker_list[i]. We can't
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
108 # do that in Tkinter so we set the global variable. (This works).
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
109 demo_maker.set(maker_list[i])
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
110
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
111 def validate_maker(w):
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
112 try:
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 22345
diff changeset
113 i = maker_list.index(demo_maker.get())
17804
49e71a83f790 [].index() raises ValueError if the value is not in the list, so only
Fred Drake <fdrake@acm.org>
parents: 17098
diff changeset
114 except ValueError:
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 22345
diff changeset
115 # Works here though. Why ? Beats me.
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 22345
diff changeset
116 return maker_list[0]
17098
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
117 # Works here though. Why ? Beats me.
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
118 return maker_list[i]
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
119
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
120 if __name__ == '__main__':
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
121 root = Tix.Tk()
989fed28b74f Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
122 RunSample(root)