]> SALOME platform Git repositories - tools/eficas.git/blob - Pmw/Pmw_1_2/demos/SubClassing.py
Salome HOME
Modif V6_4_°
[tools/eficas.git] / Pmw / Pmw_1_2 / demos / SubClassing.py
1 title = 'More examples of subclassing'
2
3 # Import Pmw from this directory tree.
4 import sys
5 sys.path[:0] = ['../../..']
6
7 import Tkinter
8 import Pmw
9
10 class ExtraMethods(Pmw.EntryField):
11
12     # How to subclass a Pmw megawidget when you only want to add or
13     # override methods.
14
15     def doubletext(self):
16         self.setvalue(self.getvalue() + ' ' + self.getvalue())
17
18 class OverrideInit(Pmw.EntryField):
19
20     # How to subclass a Pmw megawidget when you want to define
21     # a new __init__ method.
22
23     def __init__(self, textToAdd, parent = None, **kw):
24         self._textToAdd = textToAdd
25         apply(Pmw.EntryField.__init__, (self, parent), kw)
26
27     def addtext(self):
28         self.setvalue(self.getvalue() + ' ' + self._textToAdd)
29
30 class DefaultOptions(Pmw.EntryField):
31
32     # How to subclass a Pmw megawidget when you only want to set
33     # existing options to new default values.
34
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)
39
40 class NewOptions(Pmw.EntryField):
41
42     # How to subclass a Pmw megawidget when you want to add new options.
43
44     def __init__(self, parent=None , **kw):
45
46         # Define the megawidget options.
47         optiondefs = (
48             ('backgrounds',              None,     self._backgrounds),
49         )
50         self.defineoptions(kw, optiondefs)
51
52         # Initialise the base class (after defining the options).
53         Pmw.EntryField.__init__(self, parent)
54
55         # Check keywords and initialise options.
56         self.initialiseoptions()
57
58     def _backgrounds(self):
59         background = self['backgrounds']
60         Pmw.Color.changecolor(self.component('hull'), background)
61
62 class Demo:
63     def __init__(self, parent):
64         # Create and pack the megawidgets.
65         self._extraMethod = ExtraMethods(parent,
66                 labelpos = 'w',
67                 label_text = 'Sub class with extra method:',
68                 value = 'Hello'
69         )
70         self._overrideInit = OverrideInit('Again', parent,
71                 labelpos = 'w',
72                 label_text = 'Sub class with new __init__ method:',
73                 value = 'Hello'
74         )
75         self._defaultOptions = DefaultOptions(parent,
76                 labelpos = 'w',
77                 label_text = 'Sub class with new default options:',
78                 value = 'Hello'
79         )
80
81         self._newOptions = NewOptions(parent,
82                 labelpos = 'w',
83                 label_text = 'Sub class with new option:',
84                 value = 'Hello',
85                 backgrounds = 'white',
86         )
87
88         entries = (self._extraMethod, self._overrideInit,
89                 self._defaultOptions, self._newOptions)
90
91         for entry in entries:
92             entry.pack(fill='x', expand=1, padx=10, pady=5)
93         Pmw.alignlabels(entries)
94
95         bb = Pmw.ButtonBox(parent)
96         bb.add('Double text', command = self._doubleText)
97         bb.pack()
98         bb.add('Add text', command = self._addText)
99         bb.pack()
100         bb.add('White', command = self._changeColorWhite)
101         bb.pack()
102         bb.add('Green', command = self._changeColorGreen)
103         bb.pack()
104
105     def _doubleText(self):
106         self._extraMethod.doubletext()
107
108     def _addText(self):
109         self._overrideInit.addtext()
110
111     def _changeColorWhite(self):
112         self._newOptions.configure(backgrounds = 'white')
113
114     def _changeColorGreen(self):
115         self._newOptions.configure(backgrounds = 'green')
116
117 ######################################################################
118
119 # Create demo in root window for testing.
120 if __name__ == '__main__':
121     root = Tkinter.Tk()
122     Pmw.initialise(root)
123     root.title(title)
124
125     exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
126     exitButton.pack(side = 'bottom')
127     widget = Demo(root)
128     root.mainloop()