1 title = 'More examples of subclassing'
3 # Import Pmw from this directory tree.
5 sys.path[:0] = ['../../..']
10 class ExtraMethods(Pmw.EntryField):
12 # How to subclass a Pmw megawidget when you only want to add or
16 self.setvalue(self.getvalue() + ' ' + self.getvalue())
18 class OverrideInit(Pmw.EntryField):
20 # How to subclass a Pmw megawidget when you want to define
21 # a new __init__ method.
23 def __init__(self, textToAdd, parent = None, **kw):
24 self._textToAdd = textToAdd
25 apply(Pmw.EntryField.__init__, (self, parent), kw)
28 self.setvalue(self.getvalue() + ' ' + self._textToAdd)
30 class DefaultOptions(Pmw.EntryField):
32 # How to subclass a Pmw megawidget when you only want to set
33 # existing options to new default values.
35 def __init__(self, parent = None, **kw):
36 kw['label_foreground'] = 'blue'
37 kw['entry_background'] = 'white'
38 apply(Pmw.EntryField.__init__, (self, parent), kw)
40 class NewOptions(Pmw.EntryField):
42 # How to subclass a Pmw megawidget when you want to add new options.
44 def __init__(self, parent=None , **kw):
46 # Define the megawidget options.
48 ('backgrounds', None, self._backgrounds),
50 self.defineoptions(kw, optiondefs)
52 # Initialise the base class (after defining the options).
53 Pmw.EntryField.__init__(self, parent)
55 # Check keywords and initialise options.
56 self.initialiseoptions()
58 def _backgrounds(self):
59 background = self['backgrounds']
60 Pmw.Color.changecolor(self.component('hull'), background)
63 def __init__(self, parent):
64 # Create and pack the megawidgets.
65 self._extraMethod = ExtraMethods(parent,
67 label_text = 'Sub class with extra method:',
70 self._overrideInit = OverrideInit('Again', parent,
72 label_text = 'Sub class with new __init__ method:',
75 self._defaultOptions = DefaultOptions(parent,
77 label_text = 'Sub class with new default options:',
81 self._newOptions = NewOptions(parent,
83 label_text = 'Sub class with new option:',
85 backgrounds = 'white',
88 entries = (self._extraMethod, self._overrideInit,
89 self._defaultOptions, self._newOptions)
92 entry.pack(fill='x', expand=1, padx=10, pady=5)
93 Pmw.alignlabels(entries)
95 bb = Pmw.ButtonBox(parent)
96 bb.add('Double text', command = self._doubleText)
98 bb.add('Add text', command = self._addText)
100 bb.add('White', command = self._changeColorWhite)
102 bb.add('Green', command = self._changeColorGreen)
105 def _doubleText(self):
106 self._extraMethod.doubletext()
109 self._overrideInit.addtext()
111 def _changeColorWhite(self):
112 self._newOptions.configure(backgrounds = 'white')
114 def _changeColorGreen(self):
115 self._newOptions.configure(backgrounds = 'green')
117 ######################################################################
119 # Create demo in root window for testing.
120 if __name__ == '__main__':
125 exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
126 exitButton.pack(side = 'bottom')