Posted By

stagger on 02/24/08


Tagged

textmate python wxpython ttm thesis


Versions (?)

GUI Collector - Validator Library


 / Published in: Python
 

  1. import wx
  2. import string
  3.  
  4. class HostValidator(wx.PyValidator):
  5. def __init__(self, pyVar=None):
  6. wx.PyValidator.__init__(self)
  7. self.Bind(wx.EVT_CHAR, self.OnChar)
  8.  
  9. def Clone(self):
  10. """ Standard cloner.
  11.  
  12. Note that every validator must implement the Clone() method.
  13. """
  14. return HostValidator()
  15.  
  16. def Validate(self, win):
  17. tc = self.GetWindow() # tc == TextCtrl
  18. val = tc.GetValue()
  19. for x in val:
  20. if x not in string.digits and x not in ".:":
  21. return False
  22. return True
  23.  
  24. def OnChar(self, event):
  25. key = event.GetKeyCode()
  26. if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
  27. event.Skip()
  28. return
  29. if chr(key) in string.digits:
  30. event.Skip()
  31. return
  32. if chr(key) in ".:":
  33. event.Skip()
  34. return
  35. if not wx.Validator_IsSilent():
  36. wx.Bell()
  37. # Returning without calling even.Skip eats the event before it
  38. # gets to the text control
  39. return

Report this snippet  

You need to login to post a comment.