Search code examples
pythonwxpythonwxwidgets

Validating data in wxPython


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?


Solution

  • 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.