File: SubClassing.py

package info (click to toggle)
python-pmw 1.2-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,024 kB
  • ctags: 3,802
  • sloc: python: 17,143; makefile: 41
file content (128 lines) | stat: -rw-r--r-- 3,627 bytes parent folder | download | duplicates (5)
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
title = 'More examples of subclassing'

# Import Pmw from this directory tree.
import sys
sys.path[:0] = ['../../..']

import Tkinter
import Pmw

class ExtraMethods(Pmw.EntryField):

    # How to subclass a Pmw megawidget when you only want to add or
    # override methods.

    def doubletext(self):
	self.setvalue(self.getvalue() + ' ' + self.getvalue())

class OverrideInit(Pmw.EntryField):

    # How to subclass a Pmw megawidget when you want to define
    # a new __init__ method.

    def __init__(self, textToAdd, parent = None, **kw):
        self._textToAdd = textToAdd
	apply(Pmw.EntryField.__init__, (self, parent), kw)

    def addtext(self):
	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.
	optiondefs = (
            ('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.
	self.initialiseoptions()

    def _backgrounds(self):
	background = self['backgrounds']
        Pmw.Color.changecolor(self.component('hull'), background)

class Demo:
    def __init__(self, parent):
	# Create and pack the megawidgets.
	self._extraMethod = ExtraMethods(parent,
		labelpos = 'w',
		label_text = 'Sub class with extra method:',
                value = 'Hello'
        )
	self._overrideInit = OverrideInit('Again', parent,
		labelpos = 'w',
		label_text = 'Sub class with new __init__ method:',
                value = 'Hello'
        )
	self._defaultOptions = DefaultOptions(parent,
		labelpos = 'w',
		label_text = 'Sub class with new default options:',
                value = 'Hello'
        )

	self._newOptions = NewOptions(parent,
		labelpos = 'w',
		label_text = 'Sub class with new option:',
                value = 'Hello',
                backgrounds = 'white',
        )

	entries = (self._extraMethod, self._overrideInit,
                self._defaultOptions, self._newOptions)

	for entry in entries:
	    entry.pack(fill='x', expand=1, padx=10, pady=5)
	Pmw.alignlabels(entries)

        bb = Pmw.ButtonBox(parent)
        bb.add('Double text', command = self._doubleText)
        bb.pack()
        bb.add('Add text', command = self._addText)
        bb.pack()
        bb.add('White', command = self._changeColorWhite)
        bb.pack()
        bb.add('Green', command = self._changeColorGreen)
        bb.pack()

    def _doubleText(self):
        self._extraMethod.doubletext()

    def _addText(self):
        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__':
    root = Tkinter.Tk()
    Pmw.initialise(root)
    root.title(title)

    exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
    exitButton.pack(side = 'bottom')
    widget = Demo(root)
    root.mainloop()