Custom Event Classes

Custom event classes allow you to create more polished-seeming controls by allowing the control's user to process updates without needing to sub-class the control. However, to effectively use events, you normally need to create custom event classes. This recipe gives you some boilerplate code for creating your own custom event classes.

The previous version of this page has been subsumed by built-in wxPython functionality. As specified in the wxPython demo...

import  wx
import  wx.lib.newevent

SomeNewEvent, EVT_SOME_NEW_EVENT = wx.lib.newevent.NewEvent()
SomeNewCommandEvent, EVT_SOME_NEW_COMMAND_EVENT = wx.lib.newevent.NewCommandEvent()

You can bind the events normally via either binding syntax.

self.Bind(EVT_SOME_NEW_EVENT, self.handler)
EVT_SOME_NEW_EVENT(self, self.handler)

You can also attach arbitrary data to the event during its creation, then post it to whatever window you choose.

    #create the event
    evt = SomeNewEvent(attr1="hello", attr2=654)
    #post the event
    wx.PostEvent(target, evt)

When handling events with such arbitrary data, you can fetch the data via attributes, named the same as the names passed in during the event instance creation. That is, given the two keyword arguments passed to SomeNewEvent above...

    def handler(self, evt):
        # given the above constructed event, the following is true
        #evt.attr1 == "hello"
        #evt.attr2 == 654

CustomEventClasses (last edited 2008-03-11 10:50:25 by localhost)