我正在尝试wx.TextCtrl
根据对 SO 上另一个问题的响应编写我的第一个验证器,该问题应该执行以下操作:
所有字母都应打印为大写字母,并且文本字段应仅包含大写字母和数字。
但是,我的尝试在两个方面失败了:
当我更改我的模板(取自链接的响应)时,文本字段中不会添加任何内容。
我无法将小写字母更改为大写字母。
这是我的尝试:
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()