Search code examples
pythonwxpython

What is the difference between wx.lib.newevent.NewEvent() and wx.NewEventType()?


What is the difference between using wx.lib.newevent.NewEvent() and using wx.NewEventType() with wx.PyCommandEvent()? The code sample below uses the two different ways to create events to accomplish the same thing (passing data attached to an event). When should one be used instead of the other?

import wx
import wx.lib.newevent

MyEvent, EVT_MY_EVENT = wx.lib.newevent.NewEvent()

EVT_ANOTHER_EVENT_TYPE = wx.NewEventType()
EVT_ANOTHER_EVENT = wx.PyEventBinder(EVT_ANOTHER_EVENT_TYPE, 1)

class EventTest(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(210, 200))

        wx.Button(self, 1, 'NewEvent', (50, 10), (110, -1))
        wx.Button(self, 2, 'PyCommandEvent', (50, 60), (110, -1))
        wx.Button(self, 3, 'Close', (50, 110), (110, -1))

        self.Bind(wx.EVT_BUTTON, self.OnNewEvent, id=1)
        self.Bind(wx.EVT_BUTTON, self.OnPyCommandEvent, id=2)
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=3)

        self.Bind(EVT_MY_EVENT, self.NewEventHandler)
        self.Bind(EVT_ANOTHER_EVENT, self.PyCommandEventHandler)

        self.Centre()
        self.ShowModal()
        self.Destroy()

    def OnNewEvent(self, event):
        evt = MyEvent(SomeData=("NewEvent", 11, "aaa"))
        wx.PostEvent(self, evt)

    def OnPyCommandEvent(self, event):
        evt = wx.PyCommandEvent(EVT_ANOTHER_EVENT_TYPE, wx.ID_ANY)
        evt.SetClientData(("PyCommandEvent", 22, "bbb"))
        wx.PostEvent(self, evt)

    def NewEventHandler(self, evt=None):
        print "NewEvent Data:", evt.SomeData

    def PyCommandEventHandler(self, evt=None):
        print "PyCommandEvent Data:", evt.GetClientData()

    def OnClose(self, event):
        self.Close(True)

if __name__ == "__main__":
    app = wx.App(0)
    EventTest(None, -1, 'Event Test')
    app.MainLoop()

Solution

  • A wx.lib.newevent.NewEvent() is just an easier wxpython way thats been added to make a wx.NewEventType().

    if you have a look at the code in the module newevent you will see what it does.

    """Easy generation of new events classes and binder objects"""
    
    __author__ = "Miki Tebeka <[email protected]>"
    
    import wx
    
    #---------------------------------------------------------------------------
    
    def NewEvent():
        """Generate new (Event, Binder) tuple
            e.g. MooEvent, EVT_MOO = NewEvent()
        """
        evttype = wx.NewEventType()
    
        class _Event(wx.PyEvent):
            def __init__(self, **kw):
                wx.PyEvent.__init__(self)
                self.SetEventType(evttype)
                self.__dict__.update(kw)
    
        return _Event, wx.PyEventBinder(evttype)
    
    
    
    def NewCommandEvent():
        """Generate new (CmdEvent, Binder) tuple
            e.g. MooCmdEvent, EVT_MOO = NewCommandEvent()
        """
        evttype = wx.NewEventType()
    
        class _Event(wx.PyCommandEvent):
            def __init__(self, id, **kw):
                wx.PyCommandEvent.__init__(self, evttype, id)
                self.__dict__.update(kw)
    
        return _Event, wx.PyEventBinder(evttype, 1)