Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | # This tests Pmw option and component handling. |
2 | ||
3 | import Tkinter | |
4 | import Test | |
5 | import Pmw | |
6 | ||
7 | Test.initialise() | |
8 | ||
9 | """ | |
10 | Definitions: | |
11 | initialisation option: an option that can be set in the call | |
12 | to the constructor but not in configure() | |
13 | configuration option: an option that can be set in the call | |
14 | to the constructor and to configure() | |
15 | option: either an initialisation option or a configuration option | |
16 | ||
17 | Tests | |
18 | ----- | |
19 | in constructor: | |
20 | + define an option, its default value and whether it is an | |
21 | initialisation or a configuration option | |
22 | + set a callback function for a configuration option | |
23 | + set a different default for an option of a base class | |
24 | + set a different default for an option of a component of a base class | |
25 | + override the callback for a configuration option of a base class | |
26 | + create a component | |
27 | + create an alias for a component | |
28 | + create an alias for a sub-component | |
29 | ||
30 | calling constructor: | |
31 | + set an option | |
32 | + set an option of a base class | |
33 | + set an option of a component created in the constructor | |
34 | + set an option of an aliased component or sub-component created in | |
35 | the constructor | |
36 | + set an option of one or more components via their group name | |
37 | + use the default value of an option | |
38 | + use the default value of an option of a base class | |
39 | + use the default value of an option of a base class where the default | |
40 | value is redefined in the derived class | |
41 | ||
42 | calling configure: | |
43 | + set a configuration option | |
44 | + set a configuration option of a base class | |
45 | + set a configuration option of a component | |
46 | + set a configuration option of an aliased component or sub-component | |
47 | + set a configuration option of one or more components via their group name | |
48 | + set a configuration option with a callback | |
49 | + set a configuration option of a base class with a callback in the | |
50 | derived class | |
51 | """ | |
52 | ||
53 | class Simple(Pmw.MegaWidget): | |
54 | def __init__(self, parent = None, **kw): | |
55 | optiondefs = ( | |
56 | ('initsimple1', 'initsimple1', Pmw.INITOPT), | |
57 | ('initsimple2', 'initsimple2', Pmw.INITOPT), | |
58 | ('optsimple1', 'optsimple1', None), | |
59 | ('optsimple2', 'optsimple2', None), | |
60 | ) | |
61 | self.defineoptions(kw, optiondefs) | |
62 | Pmw.MegaWidget.__init__(self, parent) | |
63 | ||
64 | interior = self.interior() | |
65 | self._widget = self.createcomponent('widget', | |
66 | (('widgy', 'widget'),), None, | |
67 | Tkinter.Button, (interior,)) | |
68 | self._widget.grid(column=0, row=0, sticky='nsew') | |
69 | ||
70 | self.initialiseoptions() | |
71 | ||
72 | class Complex(Pmw.MegaWidget): | |
73 | def __init__(self, parent = None, **kw): | |
74 | optiondefs = ( | |
75 | ('initcomplex1', 'initcomplex1', Pmw.INITOPT), | |
76 | ('initcomplex2', 'initcomplex2', Pmw.INITOPT), | |
77 | ('optcomplex1', 'optcomplex1', None), | |
78 | ('optcomplex2', 'optcomplex2', None), | |
79 | ) | |
80 | self.defineoptions(kw, optiondefs) | |
81 | Pmw.MegaWidget.__init__(self, parent) | |
82 | ||
83 | interior = self.interior() | |
84 | self._simple = self.createcomponent('simple', | |
85 | (('widget', 'simple_widget'),), None, | |
86 | Simple, (interior,)) | |
87 | self._simple.grid(column=0, row=0, sticky='nsew') | |
88 | ||
89 | self.initialiseoptions() | |
90 | ||
91 | class Base(Pmw.MegaWidget): | |
92 | def __init__(self, parent = None, **kw): | |
93 | optiondefs = ( | |
94 | ('initbase1', 'initbase1', Pmw.INITOPT), | |
95 | ('initbase2', 'initbase2', Pmw.INITOPT), | |
96 | ('initbase3', 'initbase3', Pmw.INITOPT), | |
97 | ('optbase1', 'optbase1', self._optbase1), | |
98 | ('optbase2', 'optbase2', None), | |
99 | ('optbase3', 'optbase3', None), | |
100 | ) | |
101 | self.defineoptions(kw, optiondefs) | |
102 | Pmw.MegaWidget.__init__(self, parent) | |
103 | ||
104 | oldInterior = Pmw.MegaWidget.interior(self) | |
105 | self._widget = self.createcomponent('basesimple', | |
106 | (('widget', 'basesimple_widget'),), None, | |
107 | Simple, (oldInterior,)) | |
108 | self._widget.grid(column=0, row=0, sticky='nsew') | |
109 | ||
110 | self._child = self.createcomponent('child', | |
111 | (), 'Mygroup', | |
112 | Tkinter.Frame, (oldInterior,)) | |
113 | self._child.grid(column=0, row=1, sticky='nsew') | |
114 | ||
115 | self._groupie = self.createcomponent('groupie', | |
116 | (), 'Mygroup', | |
117 | Tkinter.Button, (oldInterior,), text = 'XXXXX') | |
118 | self._groupie.grid(column=0, row=2, sticky='nsew') | |
119 | ||
120 | self.basedummy = [] | |
121 | ||
122 | self.initialiseoptions() | |
123 | ||
124 | def _optbase1(self): | |
125 | self.basedummy.append(self['optbase1']) | |
126 | ||
127 | def getbasedummy(self): | |
128 | return self.basedummy | |
129 | ||
130 | def interior(self): | |
131 | return self._child | |
132 | ||
133 | class Derived(Base): | |
134 | def __init__(self, parent = None, **kw): | |
135 | # Define the options for this megawidget. | |
136 | optiondefs = ( | |
137 | ('initbase2', 'initbase2inderived', Pmw.INITOPT), | |
138 | ('initderived1', 'initderived1', Pmw.INITOPT), | |
139 | ('initderived2', 'initderived2', Pmw.INITOPT), | |
140 | ('optbase1', 'optbase1', self._optbase1), | |
141 | ('optderived1', 'optderived1', None), | |
142 | ('optderived2', 'optderived2', None), | |
143 | ('groupie_text', 'YYYYY', None), | |
144 | ) | |
145 | self.defineoptions(kw, optiondefs) | |
146 | ||
147 | # Initialise the base class (after defining my options). | |
148 | Base.__init__(self, parent) | |
149 | ||
150 | # Create components. | |
151 | interior = self.interior() | |
152 | self._widget = self.createcomponent('derivedcomplex', | |
153 | (('derivedsimple', 'derivedcomplex_simple'),), None, | |
154 | Complex, (interior,)) | |
155 | self._widget.grid(column=0, row=0, sticky='nsew') | |
156 | ||
157 | # Initialise instance. | |
158 | ||
159 | # Initialise instance variables. | |
160 | self.deriveddummy = [] | |
161 | ||
162 | # Check keywords and initialise options. | |
163 | self.initialiseoptions() | |
164 | ||
165 | def _optbase1(self): | |
166 | self.deriveddummy.append(self['optbase1']) | |
167 | ||
168 | def getderiveddummy(self): | |
169 | return self.deriveddummy | |
170 | ||
171 | testData = () | |
172 | ||
173 | c = Simple | |
174 | kw_1 = { | |
175 | 'hull_borderwidth' :2, | |
176 | 'hull_relief' :'sunken', | |
177 | 'hull_background' :'red', | |
178 | 'widget_text' :'simple', | |
179 | 'widgy_foreground' :'red', | |
180 | 'initsimple1' :'initsimple1_new', | |
181 | } | |
182 | tests = ( | |
183 | (c.pack, ()), | |
184 | (c.components, (), ['hull', 'widget']), | |
185 | (c.componentaliases, (), [('widgy', 'widget'),]), | |
186 | (c.options, (), [('initsimple1', 'initsimple1', 1), ('initsimple2', 'initsimple2', 1), ('optsimple1', 'optsimple1', 0), ('optsimple2', 'optsimple2', 0)]), | |
187 | (c.cget, 'initsimple1', 'initsimple1_new'), | |
188 | (c.cget, 'initsimple2', 'initsimple2'), | |
189 | (c.cget, 'optsimple1', 'optsimple1'), | |
190 | (c.cget, 'optsimple2', 'optsimple2'), | |
191 | (c.cget, 'widget_foreground', 'red'), | |
192 | ('optsimple1', 'optsimple1_new'), | |
193 | (c.cget, 'optsimple1', 'optsimple1_new'), | |
194 | ) | |
195 | testData = testData + ((c, ((tests, kw_1),)),) | |
196 | ||
197 | ||
198 | c = Complex | |
199 | kw_1 = { | |
200 | 'hull_borderwidth' : 2, | |
201 | 'hull_relief' : 'sunken', | |
202 | 'hull_background' : 'red', | |
203 | 'simple_widget_text' : 'complex', | |
204 | 'widget_foreground' : 'yellow', | |
205 | } | |
206 | tests = ( | |
207 | (c.pack, ()), | |
208 | (c.components, (), ['hull', 'simple']), | |
209 | (c.componentaliases, (), [('widget', 'simple_widget'),]), | |
210 | (c.options, (), [('initcomplex1', 'initcomplex1', 1), ('initcomplex2', 'initcomplex2', 1), ('optcomplex1', 'optcomplex1', 0), ('optcomplex2', 'optcomplex2', 0)]), | |
211 | ) | |
212 | testData = testData + ((c, ((tests, kw_1),)),) | |
213 | ||
214 | c = Base | |
215 | kw_1 = { | |
216 | 'hull_borderwidth' : 2, | |
217 | 'hull_relief' : 'sunken', | |
218 | 'hull_background' : 'red', | |
219 | 'basesimple_widget_text' : 'base', | |
220 | 'widget_foreground' : 'green', | |
221 | 'initbase1' : 'initbase1_new', | |
222 | } | |
223 | tests = ( | |
224 | (c.pack, ()), | |
225 | (c.components, (), ['basesimple', 'child', 'groupie', 'hull']), | |
226 | (c.componentaliases, (), [('widget', 'basesimple_widget'),]), | |
227 | (c.options, (), [('initbase1', 'initbase1', 1), ('initbase2', 'initbase2', 1), ('initbase3', 'initbase3', 1), ('optbase1', 'optbase1', 0), ('optbase2', 'optbase2', 0), ('optbase3', 'optbase3', 0)]), | |
228 | (c.cget, 'widget_foreground', 'green'), | |
229 | (c.cget, 'basesimple_widget_foreground', 'green'), | |
230 | (c.cget, 'basesimple_widgy_foreground', 'green'), | |
231 | ('widget_foreground', 'blue'), | |
232 | (c.cget, 'widget_foreground', 'blue'), | |
233 | (c.cget, 'basesimple_widget_foreground', 'blue'), | |
234 | (c.cget, 'basesimple_widgy_foreground', 'blue'), | |
235 | (c.cget, 'optbase1', 'optbase1'), | |
236 | (c.cget, 'groupie_text', 'XXXXX'), | |
237 | # When Test created the widget, it performed a test where it configured | |
238 | # each option. Hence, _optbase1() has been called twice: | |
239 | (c.getbasedummy, (), ['optbase1', 'optbase1']), | |
240 | ('optbase1', 'basedummy_new'), | |
241 | (c.getbasedummy, (), ['optbase1', 'optbase1', 'basedummy_new']), | |
242 | ) | |
243 | testData = testData + ((c, ((tests, kw_1),)),) | |
244 | ||
245 | ||
246 | c = Derived | |
247 | kw_1 = { | |
248 | 'hull_borderwidth' : 2, | |
249 | 'hull_relief' : 'sunken', | |
250 | 'hull_background' : 'red', | |
251 | 'basesimple_widget_text' : 'base simple', | |
252 | 'derivedcomplex_widget_text' : 'derived complex', | |
253 | 'initderived1' : 'initderived1_new', | |
254 | 'initbase1' : 'initbase1_new', | |
255 | 'optbase3' : 'optbase3_new', | |
256 | 'derivedcomplex_initcomplex1' : 'derivedcomplex_initcomplex1', | |
257 | 'derivedsimple_initsimple1' : 'derivedsimple_initsimple1', | |
258 | 'hull_cursor' : 'gumby', | |
259 | 'Mygroup_borderwidth' : 2, | |
260 | 'Mygroup_relief' : 'ridge', | |
261 | } | |
262 | tests = ( | |
263 | (c.pack, ()), | |
264 | (c.components, (), ['basesimple', 'child', 'derivedcomplex', 'groupie', 'hull']), | |
265 | (c.componentaliases, (), [('derivedsimple', 'derivedcomplex_simple'), ('widget', 'basesimple_widget'),]), | |
266 | (c.options, (), [('initbase1', 'initbase1', 1), ('initbase2', 'initbase2inderived', 1), ('initbase3', 'initbase3', 1), ('initderived1', 'initderived1', 1), ('initderived2', 'initderived2', 1), ('optbase1', 'optbase1', 0), ('optbase2', 'optbase2', 0), ('optbase3', 'optbase3', 0), ('optderived1', 'optderived1', 0), ('optderived2', 'optderived2', 0), ]), | |
267 | (c.getbasedummy, (), []), | |
268 | (c.getderiveddummy, (), ['optbase1', 'optbase1']), | |
269 | ('optbase1', 'derivedbasedummy_new'), | |
270 | (c.getbasedummy, (), []), | |
271 | (c.getderiveddummy, (), ['optbase1', 'optbase1', 'derivedbasedummy_new']), | |
272 | (c.cget, 'optbase3', 'optbase3_new'), | |
273 | ('optbase3', 'optbase3_newer'), | |
274 | (c.cget, 'optbase3', 'optbase3_newer'), | |
275 | (c.cget, 'optderived1', 'optderived1'), | |
276 | (c.cget, 'initderived1', 'initderived1_new'), | |
277 | (c.cget, 'initbase2', 'initbase2inderived'), | |
278 | (c.cget, 'initbase1', 'initbase1_new'), | |
279 | (c.cget, 'initbase3', 'initbase3'), | |
280 | (c.cget, 'groupie_text', 'YYYYY'), | |
281 | ('groupie_text', 'ZZZZZ'), | |
282 | (c.cget, 'groupie_text', 'ZZZZZ'), | |
283 | (c.cget, 'derivedcomplex_optcomplex1', 'optcomplex1'), | |
284 | ('derivedcomplex_optcomplex1', 'optcomplex1_new'), | |
285 | (c.cget, 'derivedcomplex_optcomplex1', 'optcomplex1_new'), | |
286 | (c.cget, 'derivedsimple_optsimple2', 'optsimple2'), | |
287 | ('derivedsimple_optsimple2', 'optsimple2_new'), | |
288 | (c.cget, 'derivedcomplex_simple_optsimple2', 'optsimple2_new'), | |
289 | ('derivedcomplex_simple_optsimple2', 'optsimple2_newer'), | |
290 | (c.cget, 'derivedsimple_optsimple2', 'optsimple2_newer'), | |
291 | (c.cget, 'hull_cursor', 'gumby'), | |
292 | (c.cget, 'groupie_relief', 'ridge'), | |
293 | (c.cget, 'Mygroup_relief', 'ridge'), | |
294 | ('Mygroup_relief', 'sunken'), | |
295 | (c.cget, 'groupie_relief', 'sunken'), | |
296 | (c.cget, 'Mygroup_relief', 'sunken'), | |
297 | ('groupie_relief', 'groove'), | |
298 | (c.cget, 'groupie_relief', 'groove'), | |
299 | ) | |
300 | testData = testData + ((c, ((tests, kw_1),)),) | |
301 | ||
302 | if __name__ == '__main__': | |
303 | #Test.setverbose(1) | |
304 | Test.runTests(testData) |