如何手动调用wxPython事件

时间:2016-08-31 08:20:33

标签: python wxpython

我有textCTRL(Wxpython)事件绑定到它:

self.x= wx.TextCtrl(self, -1, "")
self.x.Bind(wx.EVT_KILL_FOCUS, self.OnLeavex)

我想按照自己的意愿手动触发此事件。 我读了这个主题:wxPython: Calling an event manually但没有任何作用。

我试过了:

wx.PostEvent(self.x.GetEventHandler(), wx.EVT_KILL_FOCUS)

但它给出了:

  

TypeError:在方法' PostEvent'中,类型' wxEvent的预期参数2   &安培;'

我也尝试过:

self.x.GetEventHandler().ProcessEvent(wx.EVT_KILL_FOCUS)

哪个也不起作用。

1 个答案:

答案 0 :(得分:0)

The things like wx.EVT_KILL_FOCUS are not the event object needed here. Those are instances of wx.PyEventBinder which, as the name suggests, are used to bind events to handlers. The event object needed for the PostEvent or ProcessEvent functions will be the same type of object as what is received by the event handler functions. In this case it would be an instance of wx.FocusEvent.

When creating the event object you may also need to set the event type if that event class is used with more than one type of event. The binder object has that value to help you know what to use. You usually also need to set the ID to the ID of the window where the event originated. So for your example, it would be done something like this:

evt = wx.FocusEvent(wx.EVT_KILL_FOCUS.evtType, self.x.GetId())
wx.PostEvent(self.x.GetEventHandler(), evt)

...or...

self.x.GetEventHandler().ProcessEvent(wx.EVT_KILL_FOCUS)
相关问题