I am trying to write my first validator for wx.TextCtrl based on this response to another question on SO which should do the following:
All letters should be printed as captial letters and the text field should only contain captial letters and numbers.
However, my attempts fail in two regards:
-
When I change my template (taken from the linked response) nothing is added to the text field.
-
I cannot change small letters to capital letters.
Here is my attempt:
import wx
import string
########################################################################
class CharValidator(wx.PyValidator):
''' Validates data as it is entered into the text controls. '''
#----------------------------------------------------------------------
def __init__(self, flag):
wx.PyValidator.__init__(self)
self.flag = flag
self.Bind(wx.EVT_CHAR, self.OnChar)
#----------------------------------------------------------------------
def Clone(self):
'''Required Validator method'''
return CharValidator(self.flag)
#----------------------------------------------------------------------
def Validate(self, win):
return True
#----------------------------------------------------------------------
def TransferToWindow(self):
return True
#----------------------------------------------------------------------
def TransferFromWindow(self):
return True
#----------------------------------------------------------------------
def OnChar(self, event):
keycode = int(event.GetKeyCode())
if keycode < 256:
if keycode > 96 & keycode < 123:
keycode = keycode - 32
#print keycode
key = chr(keycode)
#print key
return
event.Skip()
########################################################################
class ValidationDemo(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY,
"Text Validation Tutorial")
panel = wx.Panel(self)
textOne = wx.TextCtrl(panel, validator=CharValidator('no-alpha'))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(textOne, 0, wx.ALL, 5)
panel.SetSizer(sizer)
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = ValidationDemo()
frame.Show()
app.MainLoop()
Actually all the mess with the validation function is unnecessary. You just need an eventhandler (note that it is sloppy written and is hard coded to the specific
textOneobject instead of takes the input object).Furthermore, it seems a good idwa to move the
InsertionPoint: