1

I have a textCTRL (Wxpython) with event binding to it:

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

I want to manually trigger this event as I wish. I read this topic: wxPython: Calling an event manually but nothing works.

I tried:

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

But it gives:

TypeError: in method 'PostEvent', expected argument 2 of type 'wxEvent &'

I also tried:

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

Which doesn't work as well.

Community
  • 1
  • 1
ban
  • 187
  • 1
  • 1
  • 10
  • 1
    Possible duplicate of [wxPython: Calling an event manually](http://stackoverflow.com/questions/747781/wxpython-calling-an-event-manually) – Sergey Gornostaev Aug 31 '16 at 08:36
  • @SergeyGornostaev Did you even read my question? I posted this topic in my question and explained that the answers there does not work and showing the errors. – ban Aug 31 '16 at 08:48

1 Answers1

1

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)
RobinDunn
  • 6,116
  • 1
  • 15
  • 14