469,926 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

Don't understand wxPython event handling

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
Jul 18 '05 #1
3 2693
I also have anouther question:

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

or

EVT_PAGE_DONE(self, self.OnPageDone)
Jul 18 '05 #2
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/w...goverview.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!

Jul 18 '05 #3
Robert wrote:
I also have anouther question:

which is the better way to register to events:
eventManager.Register(self.OnPageDone,EVT_PAGE_DON E,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!

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Grant Edwards | last post: by
12 posts views Thread by Jack Russell | last post: by
5 posts views Thread by Richard Grant | last post: by
4 posts views Thread by reggiestyles | last post: by
4 posts views Thread by StepanM | last post: by
5 posts views Thread by Klaudiusz Bryja | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.