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