Formatted Counter: date and float
#Pmw copyright
#Copyright 1997-1999 Telstra Corporation Limited, Australia
#Copyright 2000-2002 Really Good Software Pty Ltd, Australia
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is furnished
#to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
#INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
#PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
#OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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))
kw['datatype'] = 'date'
kw['entryfield_validate'] = 'date'
kw['entryfield_value'] = text
kw['labelpos'] = 'w'
apply(Pmw.Counter.__init__, (self, parent), kw)
class LabeledRealCounter(Pmw.Counter):
def __init__(self, parent=None , **kw):
# Define the validate option dictionary.
validate = {'validator' : 'real', 'min' : 0.0, 'max' : 100.0}
kw['datatype'] = 'real'
kw['entryfield_validate'] = validate
kw['entryfield_value'] = 50.0
kw['labelpos'] = 'w'
apply(Pmw.Counter.__init__, (self, parent), kw)
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)
root.title(title)
exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
exitButton.pack(side = 'bottom')
widget = Demo(root)
root.mainloop()
Related examples in the same category