我不知道爲什麼在吃這種情況下,但你可以通過結合而不是EVT_KEY_DOWN模擬同樣的事情。這裏有一個例子:
import wx
import wx.lib.masked as masked
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
control = ["Phone No", "(###) ###-#### x:###", "", 'F^-', "^\(\d{3}\) \d{3}-\d{4}", '','','']
maskText = masked.TextCtrl(self,
mask = control[1],
excludeChars = control[2],
formatcodes = control[3],
includeChars = "",
validRegex = control[4],
validRange = control[5],
choices = control[6],
choiceRequired = True,
defaultValue = control[7],
demo = True,
name = control[0],
style=wx.TE_PROCESS_ENTER)
maskText.Bind(wx.EVT_KEY_DOWN, self.onEnter)
#----------------------------------------------------------------------
def onEnter(self, event):
""""""
keycode = event.GetKeyCode()
if keycode == wx.WXK_RETURN or keycode == wx.WXK_NUMPAD_ENTER:
print "you pressed ENTER!"
event.Skip()
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Masked!")
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
它工作得很好,所以基本上使用'EVT_KEY_DOWN'會捕獲我們在鍵盤上按下的所有東西,對嗎?感謝您的解決方案 – Kyomuu
是的,非常。有時您需要使用添加的wx.WANTS_CHARS樣式,但總體而言,EVT_KEY_DOWN是一種方法。或EVT_CHAR。有關更多信息,請參閱http://www.blog.pythonlibrary.org/2009/08/29/wxpython-catching-key-and-char-events/。 –