[wxPython-users] Custom validator

Phil Mayes phil at philmayes.com
Sun Dec 31 16:20:44 PST 2006



At 06:38 PM 12/30/2006, you wrote:
>Hi friends!
>
>I want use custom validators in my app to check where input data correct 
>or disallow it. With following example is not possible to check if input 
>is correct or not, it just allow all input, even I explicitly point to use 
>only float input???
>
>import wx
>import re
>
>NUMBER = r'\d+$'
>FLOAT = r'\d+(\.\d{1,2})?$'
>
>
>class Validator(wx.PyValidator):
>
>     def __init__(self, regex=None):
>         wx.PyValidator.__init__(self)
>         self.regex = regex
>         if self.regex:
>             self.re = re.compile(regex)
>
>     def Clone(self):
>         return Validator(self.regex)
>
>     def Validate(self, win):
>         if not self.regex:
>             return True
>         window = self.GetWindow()
>         text = window.GetValue().strip()
>         if self.re.match(text):
>             return True
>         else:
>             return False
>
>     def TransferToWindow(self):
>         return true
>
>     def TransferFromWindow(self):
>         return true
>
>Other way is to use OnChar method, but how I can check where inputed char 
>fit appropriate regex, i.e is it float?
>
>Thanks!

Try:
FLOAT = r'\d+\.\d+$'





More information about the wxpython-users mailing list