我基於this response to another question on SO這應該做到以下幾點努力寫我的第一次驗證了wx.TextCtrl
:如何將wx.TextCtrl中的字母輸入轉換爲全部大寫字母?
所有的信件都打印成captial字母和文本字段應該只包含captial字母和數字。
然而,我的嘗試失敗有兩個方面:
當我改變我的模板沒有(從linked response拍攝)添加到文本字段。
我不能將小寫字母改爲大寫字母。
這裏是我的嘗試:
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()
你應該選擇回答你發佈的問題和要求的答案,而不是你自己的回答。在任何情況下,你的回答都不會解決你自己發佈的需求:首先,它不驗證任何只能轉換爲大寫的任何可以轉換的內容,其次,它不符合你突出顯示的行「文本域只應包含大寫字母和數字」。所以我建議你自動降低自己:-)。 – joaquin 2012-04-12 12:24:25
也許我的問題是錯誤的。我其實不需要驗證器。我只是想要所有的信件都在captials。而且你是對的,丟棄非數字或字符沒有實現。但是,這是我想達到的。至少它回答了文字問題(即標題)。我不想粗魯,真的很感謝你的回答。但這是我找到的答案,我發佈了它並接受它作爲參考(並沒有爲它賺取任何分數)。可以給你? – Henrik 2012-04-12 14:22:41