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
|
# Tests for Pmw megawidgets
import Tkinter
import Test
import Pmw
Test.initialise()
class TestWidget(Pmw.MegaWidget):
def __init__(self, parent = None, **kw):
# Define the megawidget options.
optiondefs = ()
self.defineoptions(kw, optiondefs)
# Initialise the base class (after defining the options).
Pmw.MegaWidget.__init__(self, parent)
# Create the components.
interior = self.interior()
self._label = self.createcomponent('label',
(), None,
Tkinter.Label, (interior,), text = 'test')
self._label.pack(side='left', padx=2)
# Check keywords and initialise options.
self.initialiseoptions(TestWidget)
def addComponent(self, nickname):
self.createcomponent(nickname,
(), None,
TestComponent, (self.interior(),), status = 'create')
def addTestWidget(self, widget, option, value):
w = self.createcomponent('test',
(), None,
widget, (self.interior(),))
apply(self.configure, (), {'test_' + option : value})
if w.__class__.__name__ not in ('Menu', 'Toplevel'):
w.pack()
if hasattr(widget, 'geometry'):
w.geometry('+100+100')
return str(w.cget(option))
def deleteTestWidget(self):
w = self.component('test')
if hasattr(widget, 'pack'):
w.pack_forget()
self.destroycomponent('test')
def packComponent(self, nickname):
self.component(nickname).pack()
def getStatusList(self, nickname):
return self.component(nickname)._statusList
def componentOption(self, nickname, option):
return self.component(nickname).cget(option)
class TestComponent(Pmw.MegaWidget):
def __init__(self, parent = None, **kw):
# Define the megawidget options.
optiondefs = (
('status', '', self._status),
)
self.defineoptions(kw, optiondefs)
# Initialise the base class (after defining the options).
Pmw.MegaWidget.__init__(self, parent)
# Create the components.
interior = self.interior()
self._label = self.createcomponent('label',
(), None,
Tkinter.Label, (interior,), text = 'test')
self._label.pack(side='left', padx=2)
self._statusList = []
# Check keywords and initialise options.
self.initialiseoptions()
def action(self, info):
self._statusList.append(info)
def _status(self):
self._statusList.append(self['status'])
c = TestWidget
tests = (
(c.pack, ()),
(c.components, (), ['hull', 'label']),
(c.addComponent, 'k0'),
(c.getStatusList, 'k0', ['create']),
(c.packComponent, 'k0'),
('k0_status', 'foo'),
(c.getStatusList, 'k0', ['create', 'foo']),
(c.addComponent, 'k1'),
(c.packComponent, 'k1'),
('k1_status', 'bar'),
(c.getStatusList, 'k1', ['create', 'bar']),
(c.addComponent, 'k2'),
(c.packComponent, 'k2'),
('k2_label_foreground', 'green'),
('hull_cursor', 'gumby'),
('k2_label_cursor', 'gumby'),
(c.componentOption, ('k2', 'label_foreground'), 'green'),
(c.componentOption, ('k2', 'label_cursor'), 'gumby'),
(c.addComponent, 'k3'),
(c.packComponent, 'k3'),
('k3_label_foreground', 'red'),
('hull_background', 'white'),
('k3_label_background', 'white'),
('hull_cursor', 'dot'),
('k3_label_cursor', 'dot'),
(c.componentOption, ('k3', 'label_foreground'), 'red'),
(c.componentOption, ('k3', 'label_background'), 'white'),
(c.componentOption, ('k3', 'label_cursor'), 'dot'),
('label_background', 'white'),
(c.destroycomponent, 'k0'),
(c.destroycomponent, 'k1'),
(c.destroycomponent, 'k2'),
(c.destroycomponent, 'k3'),
)
# Test each of the standard widgets as components.
for widget in [Tkinter.Button, Tkinter.Checkbutton, Tkinter.Entry, Tkinter.Label, Tkinter.Listbox, \
Tkinter.Menu, Tkinter.Menubutton, Tkinter.Message, Tkinter.Radiobutton, Tkinter.Scale, Tkinter.Text]:
tests = tests + (
(c.addTestWidget, (widget, 'foreground', 'blue'), 'blue'),
(c.deleteTestWidget, ())
)
for widget in [Tkinter.Canvas, Tkinter.Frame, Tkinter.Scrollbar, Tkinter.Toplevel]:
tests = tests + (
(c.addTestWidget, (widget, 'background', 'grey80'), 'grey80'),
(c.deleteTestWidget, ())
)
# Test the logical fonts.
for fontName in Pmw.logicalfontnames():
for sizeIncr in (-2, 0, 1, 2, 4, 8):
font = Pmw.logicalfont(fontName, sizeIncr)
tests = tests + (
('label_text', 'Testing font\n' + fontName + ' ' + str(sizeIncr)),
('label_font', font),
)
fontList = (
(('Helvetica', 0), {}),
(('Times', 0), {}),
(('Typewriter', 0), {}),
(('Typewriter', 0), {'width' : 'condensed'}),
(('Typewriter', -1), {'width' : 'condensed'}),
(('Fixed', 0), {}),
(('Fixed', 0), {'width' : 'condensed'}),
(('Fixed', -1), {'width' : 'condensed'}),
(('Helvetica', 2), {'slant' : 'italic'}),
(('Helvetica', 0), {'size' : 18}),
(('Helvetica', 0), {'weight' : 'bold'}),
(('Helvetica', 12), {'weight' : 'bold', 'slant' : 'italic'}),
(('Typewriter', 0), {'size' : 8, 'weight' : 'bold'}),
(('Fixed', 0), {'size' : 8, 'weight' : 'bold'}),
(('Times', 0), {'size' : 24, 'weight' : 'bold', 'slant' : 'italic'}),
)
for args, dict in fontList:
font = apply(Pmw.logicalfont, args, dict)
tests = tests + (
('label_text', 'Testing font\n' + str(args) + '\n' + str(dict)),
('label_font', font),
)
testData = ((c, ((tests, {'label_text' : 'Testing Pmw base classes'}),)),)
if __name__ == '__main__':
Test.runTests(testData)
|