Kevin Ollivier wrote:
...I was just wondering if anyone has written up some of the common
types of validators...
Simple ones only. I do like Phil's idea, though. These were written
before the wx namespace changeover, and I've yet to up-port the code,
but it may be useful regardless. Sadly I realize that these could be
refactored together for the most part, but it's part of a project that's
basically done and buried.
Here we go:
# Validator.py
from wxPython.wx import *
import string
class Number(wxPyValidator):
def __init__(self):
wxPyValidator.__init__(self)
EVT_CHAR(self, self.OnChar)
def Clone(self):
return Number()
def Validate(self, window):
control = self.GetWindow()
value = control.GetValue()
for c in value:
if c not in string.digits:
return False
return True
def OnChar(self, event):
key = event.KeyCode()
if (chr(key) in string.digits) or \
(key < WXK_SPACE) or (key == WXK_DELETE) or (key >= 255):
# let the control handle the message,
# the character typed is OK.
event.Skip()
elif not wxValidator_IsSilent():
wxBell()
FILENAME_CHARACTERS = string.digits + string.letters + ' _-'
class Filename(wxPyValidator):
def __init__(self):
wxPyValidator.__init__(self)
EVT_CHAR(self, self.OnChar)
def Clone(self):
return Filename()
def Validate(self, window):
control = self.GetWindow()
value = control.GetValue()
for c in value:
if c not in FILENAME_CHARACTERS:
return False
return True
def OnChar(self, event):
key = event.KeyCode()
if (key <= 255) and (chr(key) in FILENAME_CHARACTERS) or \
(key < WXK_SPACE) or (key == WXK_DELETE):
# let the control handle the message,
# the character typed is OK.
event.Skip()
elif not wxValidator_IsSilent():
wxBell()
-tom!
ยทยทยท
--