I have a validator to allow between 0-3 hex digitals to be entered in
a TextCtrl.
This works as far as allowing only hexdigits, but I can't seem to get
the 3 digit limit to work.
The "Validate" function within the validate class does not seem ever
to get called.
Here is the full code listing. Much of it was taken from the demo.
#!/usr/bin/python
import wx
import string
VALID_HEX = 1
ALPHA_ONLY = 2
DIGIT_ONLY = 7
class MyValidator(wx.PyValidator):
def __init__(self, flag=None, pyVar=None):
wx.PyValidator.__init__(self)
self.flag = flag
self.Bind(wx.EVT_CHAR, self.OnChar)
self.count = 0
def Clone(self):
return MyValidator(self.flag)
def Validate(self, win):
print "in val"
tc = self.GetWindow()
val = tc.GetValue()
if self.flag == ALPHA_ONLY:
for x in val:
if x not in string.letters:
return False
elif self.flag == DIGIT_ONLY:
for x in val:
if x not in string.hexdigits:
return False
elif self.flag == VALID_HEX:
if len(val) > 3:
return False
return True
return True
def OnChar(self, event):
key = event.GetKeyCode()
if key == wx.WXK_BACK:
print "Backspace, count is %d" % self.count
if self.count > 0:
print "decrement"
self.count -= 1
event.Skip()
return
if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
print "other"
event.Skip()
return
if self.flag == ALPHA_ONLY and chr(key) in string.letters:
event.Skip()
return
if self.flag == VALID_HEX and chr(key) in string.hexdigits:
event.Skip()
return
if self.flag == DIGIT_ONLY and chr(key) in string.hexdigits:
event.Skip()
return
if not wx.Validator_IsSilent():
wx.Bell()
# Returning without calling even.Skip eats the event before it
# gets to the text control
return
class TestValidate(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,title='Test Hex Validator')
p = wx.Panel(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
tc = wx.TextCtrl(p,-1,"",size=(65,25),validator =
MyValidator(VALID_HEX))
sizer.Add(tc,0,wx.ALL,5)
p.SetSizer(sizer)
sizer.Fit(p)
if __name__ == '__main__':
app=wx.App()
frame=TestValidate()
frame.Show()
app.MainLoop()
Thanks to a great help list.
···
--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en