Validating data in wxPython

ε祈祈猫儿з 提交于 2019-12-13 05:11:40

问题


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 alphanumerical TextCtrl 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!