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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
title = 'Subclassing Pmw.EntryField'
# Import Pmw from this directory tree.
import sys
sys.path[:0] = ['../../..']
import string
import time
import types
import Tkinter
import Pmw
class SpecialEntry(Pmw.EntryField):
def __init__(self, parent=None , **kw):
# Define the megawidget options.
optiondefs = (
('extravalidators', _myValidators, None),
)
self.defineoptions(kw, optiondefs)
# Initialise the base class (after defining the options).
Pmw.EntryField.__init__(self, parent)
self._converter = None
# Check keywords and initialise options.
self.initialiseoptions(SpecialEntry)
def setentry(self, text):
# Override Pmw.EntryField.setentry to pass string through
# the appropriate converter.
val = self['validate']
if type(val) == types.DictionaryType:
val = val['validator']
if _converters.has_key(val):
text = _converters[val](text, output = 0)
Pmw.EntryField.setentry(self, text)
def getentry(self):
text = self.get()
val = self['validate']
if type(val) == types.DictionaryType:
val = val['validator']
if _converters.has_key(val):
return _converters[val](text, output = 1)
else:
return text
def _date(text):
return Pmw.datevalidator(text, 'dmy', '.')
def _real(text):
return Pmw.realvalidator(text, ',')
def _dateconv(text, output = 0):
# On output, convert from dd.mm.yy to mm-dd-yy. On input, convert
# mm-dd-yy to dd.mm.yy and also from +NN+ or -NN- to date NN days
# before or after today.
if len(text) == 0:
return ''
if output:
try:
d = string.split(text, '.')
return d[1] + '-' + d[0] + '-' + d[2]
except:
return text
else:
if text[-1] == '+' or text[-1] == '-':
text = text[:-1]
if text[0] == '+' or text[0] == '-':
secondsAhead = string.atoi(text) * 3600 * 24
return time.strftime('%d.%m.%Y',
time.localtime(time.time() + secondsAhead))
try:
d = string.split(text,'-')
return d[1] + '.' + d[0] + '.' + d[2]
except:
return text
def _realconv(text, output = 0):
# Convert between DD.DD and DD,DD.
if output:
index = string.find(text, ',')
if index >= 0:
return text[:index] + '.' + text[index + 1:]
else:
return text
else:
index = string.find(text, '.')
if index >= 0:
return text[:index] + ',' + text[index + 1:]
else:
return text
_converters = {
'real' : _realconv,
'float8' : _realconv,
'date' : _dateconv
}
_myValidators = {
'date' : (_date, lambda s: Pmw.datestringtojdn(s, 'dmy', '.')),
'real' : (_real, lambda s: string.atof(_realconv(s, 1))),
'int4' : ('numeric', 'numeric'),
'oid' : ('int4', 'int4'),
'float8' : ('real', 'real'),
'varchar' : ('alphanumeric', 'alphanumeric'),
'text' : ('alphanumeric', 'alphanumeric'),
}
class Demo:
def __init__(self, parent):
# Create and pack the SpecialEntry megawidgets.
self._any = SpecialEntry(parent,
labelpos = 'w',
label_text = 'Text (max 10 chars):',
validate = {'validator' : 'text', 'max' : 10},
command = self.execute)
self._any.setentry('abc')
self._int = SpecialEntry(parent,
labelpos = 'w',
label_text = 'Int4:',
validate = 'int4')
self._int.setentry(1)
self._real = SpecialEntry(parent,
labelpos = 'w',
label_text = 'Real (max 2,5e+9):',
validate = {'validator' : 'real', 'max' : +2.5e+9},
)
self._real.setentry('+2.5e+6')
self._date = SpecialEntry(parent,
labelpos = 'w',
label_text = 'Date (dd.mm.yy):',
validate = 'date',
modifiedcommand = self.changed
)
# Set entry to one week from now, using special intput format.
self._date.setentry('+7+')
entries = (self._any, self._int, self._real, self._date)
for entry in entries:
entry.pack(fill='x', expand=1, padx=10, pady=5)
Pmw.alignlabels(entries)
self._any.component('entry').focus_set()
def changed(self):
print 'Text changed, converted value is', self._date.getentry()
def execute(self):
print 'Return pressed, value is', self._any.get()
# This implements a custom validation routine. It simply checks
# if the string is of odd length.
def custom_validate(self, text):
print 'text:', text
if len(text) % 2 == 0:
return -1
else:
return 1
######################################################################
# 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()
|