title
= 'More examples of subclassing'
# Import Pmw from this directory tree.
sys
.path
[:0] = ['../../..']
class ExtraMethods(Pmw
.EntryField
):
# How to subclass a Pmw megawidget when you only want to add or
self
.setvalue(self
.getvalue() + ' ' + self
.getvalue())
class OverrideInit(Pmw
.EntryField
):
# How to subclass a Pmw megawidget when you want to define
def __init__(self
, textToAdd
, parent
= None, **kw
):
self
._textToAdd
= textToAdd
apply(Pmw
.EntryField
.__init
__, (self
, parent
), kw
)
self
.setvalue(self
.getvalue() + ' ' + self
._textToAdd
)
class DefaultOptions(Pmw
.EntryField
):
# How to subclass a Pmw megawidget when you only want to set
# existing options to new default values.
def __init__(self
, parent
= None, **kw
):
kw
['label_foreground'] = 'blue'
kw
['entry_background'] = 'white'
apply(Pmw
.EntryField
.__init
__, (self
, parent
), kw
)
class NewOptions(Pmw
.EntryField
):
# How to subclass a Pmw megawidget when you want to add new options.
def __init__(self
, parent
=None , **kw
):
# Define the megawidget options.
('backgrounds', None, self
._backgrounds
),
self
.defineoptions(kw
, optiondefs
)
# Initialise the base class (after defining the options).
Pmw
.EntryField
.__init
__(self
, parent
)
# Check keywords and initialise options.
background
= self
['backgrounds']
Pmw
.Color
.changecolor(self
.component('hull'), background
)
def __init__(self
, parent
):
# Create and pack the megawidgets.
self
._extraMethod
= ExtraMethods(parent
,
label_text
= 'Sub class with extra method:',
self
._overrideInit
= OverrideInit('Again', parent
,
label_text
= 'Sub class with new __init__ method:',
self
._defaultOptions
= DefaultOptions(parent
,
label_text
= 'Sub class with new default options:',
self
._newOptions
= NewOptions(parent
,
label_text
= 'Sub class with new option:',
entries
= (self
._extraMethod
, self
._overrideInit
,
self
._defaultOptions
, self
._newOptions
)
entry
.pack(fill
='x', expand
=1, padx
=10, pady
=5)
bb
= Pmw
.ButtonBox(parent
)
bb
.add('Double text', command
= self
._doubleText
)
bb
.add('Add text', command
= self
._addText
)
bb
.add('White', command
= self
._changeColorWhite
)
bb
.add('Green', command
= self
._changeColorGreen
)
self
._extraMethod
.doubletext()
self
._overrideInit
.addtext()
def _changeColorWhite(self
):
self
._newOptions
.configure(backgrounds
= 'white')
def _changeColorGreen(self
):
self
._newOptions
.configure(backgrounds
= 'green')
######################################################################
# Create demo in root window for testing.
if __name__
== '__main__':
exitButton
= Tkinter
.Button(root
, text
= 'Exit', command
= root
.destroy
)
exitButton
.pack(side
= 'bottom')