Validating user input
There may be times where you need to validate the input of a field before accepting a value from users. This validation could be done manually in the close handler for the dialog, but this leads to lots of duplicated and specialized code that would need to be copied to other dialogs. In order to help avoid this and provide a common way of validating user input, the wxPython library provides a Validator
API that allows you to create custom Validator
objects that can be assigned to controls. When a dialog's Validate
method is called, all child controls of the dialog have their validation methods called as well to validate the input. In this recipe, we will discuss how to make a custom validator that can be used in TextCtrl
.
How to do it…
Perform the following steps:
First, we will create a subclass of
wx.PyValidator
to implement our field-checking logic:class InputValidator(wx.PyValidator): def __init__(self, checker = lambda s: s): super(InputValidator, self...