Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Old topic, but I think I've got this figured out after being confused about it for a long time, so if anyone else comes through here looking for the answer, this might help.</p> <p>To manually post an event, you can use</p> <pre><code>self.GetEventHandler().ProcessEvent(event) </code></pre> <p>(wxWidgets docs <a href="http://docs.wxwidgets.org/stable/wx_wxevthandler.html#wxevthandlerprocessevent" rel="noreferrer">here</a>, wxPython docs <a href="http://www.wxpython.org/docs/api/wx.EvtHandler-class.html" rel="noreferrer">here</a>)</p> <p>or</p> <pre><code>wx.PostEvent(self.GetEventHandler(), event) </code></pre> <p>(<a href="http://docs.wxwidgets.org/stable/wx_miscellany.html#wxpostevent" rel="noreferrer">wxWidgets docs</a>, <a href="http://www.wxpython.org/docs/api/wx-module.html#PostEvent" rel="noreferrer">wxPython docs</a>) </p> <p>where <code>event</code> is the event you want to post. Construct the event with e.g.</p> <pre><code>wx.PyCommandEvent(wx.EVT_BUTTON.typeId, self.GetId()) </code></pre> <p>if you want to post a EVT_BUTTON event. Making it a <a href="http://www.wxpython.org/docs/api/wx.PyCommandEvent-class.html" rel="noreferrer">PyCommandEvent</a> means that it will propagate upwards; other event types don't propagate by default.</p> <p>You can also create custom events that can carry whatever data you want them to. Here's an example:</p> <pre><code>myEVT_CUSTOM = wx.NewEventType() EVT_CUSTOM = wx.PyEventBinder(myEVT_CUSTOM, 1) class MyEvent(wx.PyCommandEvent): def __init__(self, evtType, id): wx.PyCommandEvent.__init__(self, evtType, id) myVal = None def SetMyVal(self, val): self.myVal = val def GetMyVal(self): return self.myVal </code></pre> <p>(I think I found this code in a mailing list archive somewhere, but I can't seem to find it again. If this is your example, thanks! Please add a comment and take credit for it!)</p> <p>So now, to Post a custom event:</p> <pre><code>event = MyEvent(myEVT_CUSTOM, self.GetId()) event.SetMyVal('here is some custom data') self.GetEventHandler().ProcessEvent(event) </code></pre> <p>and you can bind it just like any other event</p> <pre><code>self.Bind(EVT_CUSTOM, self.on_event) </code></pre> <p>and get the custom data in the event handler</p> <pre><code>def on_event(self, e): data = e.GetMyVal() print 'custom data is: {0}'.format(data) </code></pre> <p>Or include the custom data in the event constructor and save a step:</p> <pre><code>class MyEvent(wx.PyCommandEvent): def __init__(self, evtType, id, val = None): wx.PyCommandEvent.__init__(self, evtType, id) self.myVal = val </code></pre> <p>etc.</p> <p>Hope this is helpful to someone.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload