Don't understand wxPython event handling

R

Robert

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
 
R

Robert

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)
 
R

Robin Dunn

Robert said:
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()
 
R

Robin Dunn

Robert said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top