Hi all,
I'd like to make a wx.TextCtrl widget that ignores invalid keypresses,
and I'm wondering if validators are the way to go. This was my first
attempt:
nameRE = re.compile(r'^[a-zA-Z]\w*$')
class nameValidator(wx.PyValidator):
def __init__(self, *args, **kw):
wx.PyValidator.__init__(self, *args, **kw)
def Clone(self):
return nameValidator()
def Validate(self, parent):
val = self.GetWindow().GetValue()
return bool(not val or nameRE.match(val))
Then, create the TextCtrl:
self.name = wx.TextCtrl(self)
self.name.SetValidator(nameValidator())
But no dice --- the user was still able to type anything they wished
into the TextCtrl.
I googled around, and eventually found an example of the sort of thing
I wanted to do:
http://groups.google.co.nz/group/fr.comp.lang.python/msg/c712e9d3865119e7?hl=en&
This demonstrates a TextCtrl where the user may only enter floats.
Any alphabetic characters (for instance) will be ignored. So, that's
great. But, looking at the code, I can't see what role the validator
plays. If I grab the OnChar method from the Validator and bind it to
the TextCtrl directly, I get the same behaviour, with no validator in
sight.
So ... Is that the best way to do things? (ie: bind to EVT_CHAR and
call .Skip() if we don't like the character)
Or is there some way of using validators that I'm missing?
···
--
John.