Don't understand wxPython event handling

Discussion in 'Python' started by Robert, Apr 1, 2004.

  1. Robert

    Robert Guest

    Hello list,

    could somebody point me to a good reference about wxPython event handling?
    I have seen many examples but which one is the best. Waht are the advantages
    and disadvantages?

    Can you also have a short look at the example below and give me some
    comments, please?

    Example:
    I have implemented (or copied from somewhere) one event in two flavours.
    Both work, but which one is the best? Or does anybody have a better
    implementation.

    EVT_NEXT_PAGE_ID = wxNewId()

    def EVT_NEXT_PAGE( win, func ):
    """Your documentation here"""
    win.Connect( -1, -1, EVT_NEXT_PAGE_ID, func )

    class showNextPageEvent(wxPyEvent):
    def __init__(self, windowID):
    wxPyEvent.__init__(self)
    self.SetEventType(EVT_NEXT_PAGE_ID)


    or

    EVT_NEXT_PAGE_TYPE = wxNewEventType()

    def EVT_NEXT_PAGE( window, function ):
    """Your documentation here"""
    window.Connect( -1, -1, EVT_NEXT_PAGE_TYPE, function )

    class showNextPageEvent(wxPyCommandEvent):
    eventType = EVT_NEXT_PAGE_TYPE
    def __init__(self, windowID):
    wxPyCommandEvent.__init__(self, self.eventType, windowID)
    def Clone( self ):
    self.__class__( self.GetId() )


    Thank you

    Robert
    Robert, Apr 1, 2004
    #1
    1. Advertising

  2. Robert

    Robert Guest

    I also have anouther question:

    which is the better way to register to events:
    eventManager.Register(self.OnPageDone,EVT_PAGE_DONE,self.pageContainer)

    or

    EVT_PAGE_DONE(self, self.OnPageDone)
    Robert, Apr 1, 2004
    #2
    1. Advertising

  3. Robert

    Robin Dunn Guest

    Robert wrote:
    > Hello list,
    >
    > could somebody point me to a good reference about wxPython event handling?
    > I have seen many examples but which one is the best. Waht are the advantages
    > and disadvantages?


    http://wiki.wxpython.org/index.cgi/RecipesEvents
    http://wxwidgets.org/manuals/2.5.1/wx_eventhandlingoverview.html
    The wxPython demo
    etc.

    >
    > Can you also have a short look at the example below and give me some
    > comments, please?
    >
    > Example:
    > I have implemented (or copied from somewhere) one event in two flavours.
    > Both work, but which one is the best? Or does anybody have a better
    > implementation.
    >
    > EVT_NEXT_PAGE_ID = wxNewId()


    wxNewId is meant for window/menuItem/toolbarItem IDs. wxNewEventType
    should be used for, uh, new eventTypes ;-)


    >
    > def EVT_NEXT_PAGE( win, func ):
    > """Your documentation here"""
    > win.Connect( -1, -1, EVT_NEXT_PAGE_ID, func )
    >
    > class showNextPageEvent(wxPyEvent):
    > def __init__(self, windowID):
    > wxPyEvent.__init__(self)
    > self.SetEventType(EVT_NEXT_PAGE_ID)
    >


    This is fine (or deriving from wxPyCommandEvent if you want the event to
    propgate to parent windows,) but even better is to use the newevent
    module in the wxPython library. It contains functions that generate on
    the fly an event class and binder function similar to the above for you,
    but it will help your code to be future proof in case things change in
    future releases.

    import wx.lib.newevent
    showNextPageEvent, EVT_NEXT_PAGE = wx.lib.newevent.NewEvent()

    --
    Robin Dunn
    Software Craftsman
    http://wxPython.org Java give you jitters? Relax with wxPython!
    Robin Dunn, Apr 1, 2004
    #3
  4. Robert

    Robin Dunn Guest

    Robert wrote:
    > I also have anouther question:
    >
    > which is the better way to register to events:
    > eventManager.Register(self.OnPageDone,EVT_PAGE_DONE,self.pageContainer)
    >
    > or
    >
    > EVT_PAGE_DONE(self, self.OnPageDone)
    >
    >


    Short answer: It depends.

    Long answer: The eventManager sits on top of a Publish/Subscribe
    framework (Observer design pattern) so it is useful if you want to have
    lots of handlers for a specific event and/or if you want your handlers
    to be loosly coupled with the windows where the event was generated. If
    you put your handlers in the same class as the window that generated the
    event (or a parent window) then using the simpler form is just fine.

    --
    Robin Dunn
    Software Craftsman
    http://wxPython.org Java give you jitters? Relax with wxPython!
    Robin Dunn, Apr 1, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. simon

    Error message I don't understand

    simon, Dec 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    315
    Hatim R
    Dec 5, 2003
  2. m miller

    don't understand error

    m miller, Jan 9, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,732
    smitdso
    Sep 8, 2006
  3. Wayne  Wengert

    Don't Understand

    Wayne Wengert, May 1, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    369
    Wayne Wengert
    May 1, 2004
  4. Grant Edwards

    Don't understand wxPython ids

    Grant Edwards, Apr 27, 2004, in forum: Python
    Replies:
    19
    Views:
    1,558
    Roger Binns
    Apr 30, 2004
  5. John Ladasky
    Replies:
    2
    Views:
    241
    John Ladasky
    Apr 28, 2007
Loading...

Share This Page