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
|
title = 'Subclassing Pmw.Counter'
# Import Pmw from this directory tree.
import sys
sys.path[:0] = ['../../..']
import string
import time
import types
import Tkinter
import Pmw
class LabeledDateCounter(Pmw.Counter):
def __init__(self, parent=None , **kw):
# Need to use long ints here because on the Macintosh the maximum size
# of an integer is smaller than the value returned by time.time().
now = (long(time.time()) / 300) * 300
text = time.strftime('%y/%m/%d', time.localtime(now))
# Define the megawidget options.
optiondefs = (
('datatype', 'date', None),
('entryfield_validate', 'date', None),
('entryfield_value', text, None),
('labelpos', 'w', None),
)
self.defineoptions(kw, optiondefs)
# Initialise the base class (after defining the options).
Pmw.Counter.__init__(self, parent)
# Check keywords and initialise options.
self.initialiseoptions(LabeledDateCounter)
class LabeledRealCounter(Pmw.Counter):
def __init__(self, parent=None , **kw):
# Define the validate option dictionary.
validate = {'validator' : 'real', 'min' : 0.0, 'max' : 100.0}
# Define the megawidget options.
optiondefs = (
('datatype', 'real', None),
('entryfield_validate', validate, None),
('entryfield_value', 50.0, None),
('labelpos', 'w', None),
)
self.defineoptions(kw, optiondefs)
# Initialise the base class (after defining the options).
Pmw.Counter.__init__(self, parent)
# Check keywords and initialise options.
self.initialiseoptions(LabeledRealCounter)
class Demo:
def __init__(self, parent):
# Create and pack some LabeledDateCounters and LabeledRealCounter.
self._date1 = LabeledDateCounter(parent, label_text = 'Date:')
self._date2 = LabeledDateCounter(parent, label_text = 'Another Date:')
self._real1 = LabeledRealCounter(parent, label_text = 'Real:')
self._real2 = LabeledRealCounter(parent, label_text = 'Another Real:')
counters = (self._date1, self._date2, self._real1, self._real2)
for counter in counters:
counter.pack(fill='x', expand=1, padx=10, pady=5)
Pmw.alignlabels(counters)
######################################################################
# Create demo in root window for testing.
if __name__ == '__main__':
root = Tkinter.Tk()
Pmw.initialise(root, fontScheme = 'pmw1')
root.title(title)
exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
exitButton.pack(side = 'bottom')
widget = Demo(root)
root.mainloop()
|