wxPython:将wx.TE_PROCESS_ENTER放在Masked TextCtrl上

时间:2012-08-26 07:52:51

标签: wxpython textctrl

在Google上搜索我发现在屏蔽的TextCtrl上使用wx.TE_PROCESS_ENTER是不可能的,我自己尝试设置style=wx.TE_PROCESS_ENTER然后将其与wx.EVT_TEXT_ENTER绑定,但没有任何反应。我试图做的是制作许多蒙版的TextCtrl,这样用户可以输入一些值,然后在输入按钮时按下一个函数来调用它。

提前致谢

2 个答案:

答案 0 :(得分:1)

我不确定为什么会吃那个事件,但你可以通过绑定到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()

答案 1 :(得分:0)

按照这个例子,我可以使它工作

https://www.programcreek.com/python/example/4695/wx.TE_PROCESS_ENTER

def __init__(self):
        wx.Frame.__init__(self, None,
                          pos=wx.DefaultPosition, size=wx.Size(450, 100),
                          style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION |
                          wx.CLOSE_BOX | wx.CLIP_CHILDREN,
                          title="BRUNO")
        panel = wx.Panel(self)

        ico = wx.Icon('boy.ico', wx.BITMAP_TYPE_ICO)
        self.SetIcon(ico)

        my_sizer = wx.BoxSizer(wx.VERTICAL)
        lbl = wx.StaticText(panel,
                            label="Bienvenido Sir. How can I help you?")
        my_sizer.Add(lbl, 0, wx.ALL, 5)
        self.txt = wx.TextCtrl(panel, style=wx.TE_PROCESS_ENTER,
                               size=(400, 30))
        self.txt.SetFocus()
        self.txt.Bind(wx.EVT_TEXT_ENTER, self.OnEnter)
        my_sizer.Add(self.txt, 0, wx.ALL, 5)
        panel.SetSizer(my_sizer)
        self.Show()
        speak.Speak('''Welcome back Sir, Broono at your service.''') 

此网站还说明,如果您尚未将样式设置为wx.TE_PROCESS_ENTER,则不能使用EVT_TEXT_ENTER

https://wxpython.org/Phoenix/docs/html/wx.TextCtrl.html