Hi,
I would like to check the user input in a ComboBox.
For this I try to use a validator.
After some searching on the web I found an example
from which I made the code below.
I would like to limit the user input to two digits
representing a value between 0 to 99.
I see no effect?
Linux 2.6.22.19-0.1-default
Python 2.5.1
wx.Python 2.8.7.1
···
-------------------------
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Validator example
# Muk 05.02.2009: start
"""validator example"""
import wx
class Validator(wx.Frame):
"""frame contains a ComboBox with validator"""
def __init__(self):
# """frame contains a ComboBox with validator"""
wx.Frame.__init__(self, None, wx.ID_ANY, "my frame")
self.panel = wx.Panel(self, wx.ID_ANY)
# controls
val99 = Int99Validator
self.cbx_validator = wx.ComboBox( self.panel, wx.ID_ANY, choices=['0'], size=(150, 30), validator=val99())
self.txt_validator = wx.StaticText( self.panel, wx.ID_ANY, label='validator', size=(150, 30), style=wx.ALIGN_RIGHT)
self.siz_top = wx.BoxSizer(wx.VERTICAL)
self.siz_top.Add(self.txt_validator, 0, wx.ALL, 5)
self.siz_top.Add(self.cbx_validator, 0, wx.ALL, 5)
# bindings
self.Bind(wx.EVT_TEXT, self.on_cbx_validator, self.cbx_validator)
# layout
self.panel.SetAutoLayout(True)
self.panel.SetSizer(self.siz_top)
self.panel.Layout()
def on_cbx_validator(self, evt):
"""EVT_TEXT in cbx_validator"""
value = self.cbx_validator.GetValue()
print("on_cbx_validator out: value=%s" % (str(value)))
evt.Skip()
class Int99Validator(wx.PyValidator):
"""Validator to check if GetValue() provides an integer value 0..99"""
def Clone(self):
"""Clone ???"""
return self.__class__()
def Validate(self, window):
"""Validate ???"""
# window = wx.PyTypeCast(window, "wx.ComboBox") # pylint says: "Module 'wx' has no 'PyTypeCast' member"
try:
value = int(window.GetValue(), 10)
if value < 0:
raise ValueError
if value > 99:
raise ValueError
return True
except ValueError:
return False
if __name__ == "__main__":
APP = wx.PySimpleApp()
FRAME = Validator().Show()
APP.MainLoop()
-------------------------
Thanks in advance
--
Kurt Mueller