问题
I'm trying to create Validators for inputs on forms. I learned already that in wxPython it is necessary to inherit from wx.Validator due to lack of support for standard wxTextValidator and others.
My question is:
how effectively check that string complies to simple rules (no regexp please)
acceptableChars = ['a', 'b', ...]
all(char in acceptableChars for char in string)
is something like this efficient? and how to cleanly specify all alphanumeric or digits? or maybe is there any ready class or function?
will overriding
Validate
method only keep the constraints while inputing data - I mean will it prevent user from entering digits into alphanumericalTextCtrl
or will it check only at closing the modal diagog?
回答1:
Validate()
is called only when the dialog is about to close by default, but you may also call it yourself when the control loses focus. Finally, if your control doesn't accept some characters at all, you can also intercept wxEVT_CHAR
events to prevent them from being entered. I do believe wxPython demo shows how to do it.
回答2:
"12345".isdigit() # True
"123.45".isdigit() # False
"abcde".isalpha() # True
"abcde1".isalpha() # False
"abcde12345".isalnum() # True
"!!??".isalnum() # False
for other situations you have to use you code
acceptableChars = "ab5-?" # or acceptableChars = ['a', 'b', '5', '-', '?']
all(char in acceptableChars for char in string)
.
def isValid(string, acceptableChars):
return all(char in acceptableChars for char in string)
来源:https://stackoverflow.com/questions/19759096/validating-data-in-wxpython