title
= 'Subclassing Pmw.EntryField'
# Import Pmw from this directory tree.
sys
.path
[:0] = ['../../..']
class SpecialEntry(Pmw
.EntryField
):
def __init__(self
, parent
=None , **kw
):
kw
['extravalidators'] = _myValidators
apply(Pmw
.EntryField
.__init
__, (self
, parent
), kw
)
def setentry(self
, text
):
# Override Pmw.EntryField.setentry to pass string through
# the appropriate converter.
if type(val
) == types
.DictionaryType
:
if _converters
.has_key(val
):
text
= _converters
[val
](text
, output
= 0)
Pmw
.EntryField
.setentry(self
, text
)
if type(val
) == types
.DictionaryType
:
if _converters
.has_key(val
):
return _converters
[val
](text
, output
= 1)
return Pmw
.datevalidator(text
, 'dmy', '.')
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
d
= string
.split(text
, '.')
return d
[1] + '-' + d
[0] + '-' + d
[2]
if text
[-1] == '+' or 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
))
d
= string
.split(text
,'-')
return d
[1] + '.' + d
[0] + '.' + d
[2]
def _realconv(text
, output
= 0):
# Convert between DD.DD and DD,DD.
index
= string
.find(text
, ',')
return text
[:index
] + '.' + text
[index
+ 1:]
index
= string
.find(text
, '.')
return text
[:index
] + ',' + text
[index
+ 1:]
'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'),
def __init__(self
, parent
):
# Create and pack the SpecialEntry megawidgets.
self
._any
= SpecialEntry(parent
,
label_text
= 'Text (max 10 chars):',
validate
= {'validator' : 'text', 'max' : 10},
self
._any
.setentry('abc')
self
._int
= SpecialEntry(parent
,
self
._real
= SpecialEntry(parent
,
label_text
= 'Real (max 2,5e+9):',
validate
= {'validator' : 'real', 'max' : +2.5e+9},
self
._real
.setentry('+2.5e+6')
self
._date
= SpecialEntry(parent
,
label_text
= 'Date (dd.mm.yy):',
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
)
entry
.pack(fill
='x', expand
=1, padx
=10, pady
=5)
self
._any
.component('entry').focus_set()
print 'Text changed, converted value is', self
._date
.getentry()
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
):
######################################################################
# Create demo in root window for testing.
if __name__
== '__main__':
exitButton
= Tkinter
.Button(root
, text
= 'Exit', command
= root
.destroy
)
exitButton
.pack(side
= 'bottom')