将一个小部件绑定到 EVT_TEXT 事件以在 wxpython 中触发多个事件处理程序

binding one widget to EVT_TEXT event to trigger more than one event handlers in wxpython

在我的示例中,将一些数据输入 wx.TextCtrl 小部件必须触发两个事件处理函数。但第一个绑定被最后一个绑定覆盖。

self.tc_limit.Bind(wx.EVT_TEXT, self.calculate_registration_fee)
self.tc_limit.Bind(wx.EVT_TEXT, self.catch_errors)

在我的情况下,当异常发生时,最后的绑定有效,但在向小部件输入正确的数据后,它不会触发第一次绑定

那我该怎么做呢

像这样使用event.Skip()

import wx
def onText1(event):
    print "First Text1!"
    event.Skip()

def onText2(event):
    print "Then Text2!"

app = wx.App()

frame = wx.Frame(None, -1, '')
box = wx.StaticBox(frame, -1, "")
sizer = wx.StaticBoxSizer(box, orient=wx.VERTICAL)
text0 = wx.StaticText(frame,label="1st Item")
text0_input = wx.TextCtrl(frame, wx.ID_ANY, size=(345,25))
text0_input.SetValue("")
sizer.Add(text0, wx.ALIGN_LEFT|wx.ALL, border=10)
sizer.Add(text0_input, wx.ALIGN_LEFT|wx.ALL, border=10)
text0_input.Bind(wx.EVT_TEXT, onText2)
text0_input.Bind(wx.EVT_TEXT, onText1)

main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(sizer)
frame.SetSizer(main_sizer)

frame.Centre()
frame.Show()
text0_input.SetFocus()
window_focus = True

app.MainLoop()

event.Skip()
"This method can be used inside an event handler to control whether further event handlers bound to this event will be called after the current one returns. Without Skip() (or equivalently if Skip(False) is used), the event will not be processed any more. If Skip(True) is called, the event processing system continues searching for a further handler function for this event, even though it has been processed already in the current handler."