this code style is a little confusing (about binding events)

hello..

I see that its pretty common for wxpythonistas to use this naming style while binding events to methods..

Object.Bind(wx.SOME_EVENT, self.SomeHandler)
....

def SomeHandler(self, event):
.....

as a new wxpython programmer, from the naming style I believe that the wx.SOME_EVENT is being passed as an argument to the SomeHandler() method. But in reality (according to the book "wxpython in action" page 65) what is being passed as an argument is the object itself so why not use this naming style instead:

def SomeHandler(self, object):

There seems to be a bit of misunderstanding there. Maybe this will help:

1. The wx.SOME_EVENT is an object that represents the type or *kind* of event that a handler is being bound for. For example, the type of event generated for a button widget being activated is represented by wx.EVT_BUTTON. wx.EVT_BUTTON is not the event itself.

2. The object being passed to the handler is an instance of a class that holds relevant information about the actual event that happened. For example, an instance of wx.CommandEvent is passed for a button activation.

3. The object that the event originated from can be fetched using event.GetEventObject(). For example, for wx.EVT_BUTTON events GetEventObject will give you the button widget that was activated.

···

On 4/15/11 7:45 PM, PythonJourney wrote:

hello..

I see that its pretty common for wxpythonistas to use this naming style
while binding events to methods..

Object.Bind(wx.SOME_EVENT, self.SomeHandler)
....

def SomeHandler(self, event):
.....

as a new wxpython programmer, from the naming style I believe that the
wx.SOME_EVENT is being passed as an argument to the SomeHandler()
method. But in reality (according to the book "wxpython in action" page
65) what is being passed as an argument is the object itself so why not
use this naming style instead:

def SomeHandler(self, object):

--
Robin Dunn
Software Craftsman

thanks for answering!..

I feel pretty stupid for saying this but I dont understand whats happening when the Bind() function its called...

If I have something like this:

SomeObject.Bind(wx.SOME_EVENT, self.SomeHandler)

I believe that internally the Bind() method its calling the SomeHandler() method. What I wanna know its how is this being called?, is it called like this?:

self.SomeHandler(self, SomeObject)

from your reply I believe thats not whats happening, because otherwise I wouldn't need to use the GetEventObject to get the object later..

So, my doubt is, what is that second argument being pass?

El 16/04/2011 12:04 a.m., Robin Dunn escribi�:

···

On 4/15/11 7:45 PM, PythonJourney wrote:

hello..

I see that its pretty common for wxpythonistas to use this naming style
while binding events to methods..

Object.Bind(wx.SOME_EVENT, self.SomeHandler)
....

def SomeHandler(self, event):
.....

as a new wxpython programmer, from the naming style I believe that the
wx.SOME_EVENT is being passed as an argument to the SomeHandler()
method. But in reality (according to the book "wxpython in action" page
65) what is being passed as an argument is the object itself so why not
use this naming style instead:

def SomeHandler(self, object):

There seems to be a bit of misunderstanding there. Maybe this will help:

1. The wx.SOME_EVENT is an object that represents the type or *kind* of event that a handler is being bound for. For example, the type of event generated for a button widget being activated is represented by wx.EVT_BUTTON. wx.EVT_BUTTON is not the event itself.

2. The object being passed to the handler is an instance of a class that holds relevant information about the actual event that happened. For example, an instance of wx.CommandEvent is passed for a button activation.

3. The object that the event originated from can be fetched using event.GetEventObject(). For example, for wx.EVT_BUTTON events GetEventObject will give you the button widget that was activated.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

thanks for answering!..

I feel pretty stupid for saying this but I dont understand whats
happening when the Bind() function its called...

If I have something like this:

SomeObject.Bind(wx.SOME_EVENT, self.SomeHandler)

I believe that internally the Bind() method its calling the
SomeHandler() method. What I wanna know its how is this being
called?, is it called like this?:

self.SomeHandler(self, SomeObject)

No, I believe that the Bind function is adding an entry to an event
handle table for the object/window/application/os, (depending on the
scope of events of type wx.SOME_EVENT, that says that in the case of
this object/window/application/the os receiving an event of type
wx.SOME_EVENT then the function to call is self.SomeHandler(event) and
it process it. As this table can have multiple handlers for a given
event type all the event handlers for that event type will be called
unless one of them calls veto() on the event to say it must not go
anywhere.

Hope this helps.

Gadget/Steve

···

On 16/04/2011 7:20 AM, PythonJourney wrote:

from your reply I believe thats not whats happening, because
otherwise I wouldn't need to use the GetEventObject to get the
object later..

So, my doubt is, what is that second argument being pass?

El 16/04/2011 12:04 a.m., Robin Dunn escribi�:

On 4/15/11 7:45 PM, PythonJourney wrote:

hello..

I see that its pretty common for wxpythonistas to use this naming
style
while binding events to methods..

Object.Bind(wx.SOME_EVENT, self.SomeHandler)
....

def SomeHandler(self, event):
.....
.....

as a new wxpython programmer, from the naming style I believe that
the
wx.SOME_EVENT is being passed as an argument to the SomeHandler()
method. But in reality (according to the book "wxpython in action"
page
65) what is being passed as an argument is the object itself so
why not
use this naming style instead:

def SomeHandler(self, object):

There seems to be a bit of misunderstanding there. Maybe this will
help:

1. The wx.SOME_EVENT is an object that represents the type or
*kind* of event that a handler is being bound for. For example,
the type of event generated for a button widget being activated is
represented by wx.EVT_BUTTON. wx.EVT_BUTTON is not the event itself.

2. The object being passed to the handler is an instance of a class
that holds relevant information about the actual event that
happened. For example, an instance of wx.CommandEvent is passed
for a button activation.

3. The object that the event originated from can be fetched using
event.GetEventObject(). For example, for wx.EVT_BUTTON events
GetEventObject will give you the button widget that was activated.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

thanks for answering!..

I feel pretty stupid for saying this but I dont understand whats
happening when the Bind() function its called...

If I have something like this:

SomeObject.Bind(wx.SOME_EVENT, self.SomeHandler)

I believe that internally the Bind() method its calling the
SomeHandler() method. What I wanna know its how is this being
called?, is it called like this?:

self.SomeHandler(self, SomeObject)

No, I believe that the Bind function is adding an entry to an event
handle table for the object/window/application

Correct, it simply adds to a table of event binding info. The self.SomeHandler is *not* called at the time of the Bind, it is called later when an event happens that matches the table information, (source, ID, type, etc. ) This is what "event-driven programming" is all about. You tell the framework want you want it to do when something happens, and then sit back and wait for the something to happen.

from your reply I believe thats not whats happening, because
otherwise I wouldn't need to use the GetEventObject to get the
object later..

[1]

So, my doubt is, what is that second argument being pass?

The object passed to SomeHandler is constructed at the time time that the event happens, just before the call to SomHandler is made. It is an instance of some class derived from wx.Event and it simply holds information about the event and where it came from. For example for a wx.EVT_KEY_DOWN you'll get a wx.KeyEvent that holds information about the key that was pressed, the current state of the modifier keys, etc. For a wx.EVT_SIZE you'll get an instance of wx.SizeEvent that can give you the new size of the window.

Try doing a "print event" in your handlers to see what you are getting.

[1] Just for the record you won't need to use GetEventObject most of the time, but it is helpful when you may be binding the same handler function for events that may be originating from multiple source objects. For example, if I do:

  self.Bind(wx.EVT_BUTTON, self.OnButton)

without telling it which button I want to get events from, then OnButton will be called for *any* button that is within the scope of (children, grandchildren of) the self window.

···

On 4/16/11 3:24 AM, Gadget/Steve wrote:

On 16/04/2011 7:20 AM, PythonJourney wrote:

--
Robin Dunn
Software Craftsman

Thanks again Robin for the explication.

El 16/04/2011 02:51 p.m., Robin Dunn escribi�:

···

On 4/16/11 3:24 AM, Gadget/Steve wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 16/04/2011 7:20 AM, PythonJourney wrote:

thanks for answering!..

I feel pretty stupid for saying this but I dont understand whats
happening when the Bind() function its called...

If I have something like this:

SomeObject.Bind(wx.SOME_EVENT, self.SomeHandler)

I believe that internally the Bind() method its calling the
SomeHandler() method. What I wanna know its how is this being
called?, is it called like this?:

self.SomeHandler(self, SomeObject)

No, I believe that the Bind function is adding an entry to an event
handle table for the object/window/application

Correct, it simply adds to a table of event binding info. The self.SomeHandler is *not* called at the time of the Bind, it is called later when an event happens that matches the table information, (source, ID, type, etc. ) This is what "event-driven programming" is all about. You tell the framework want you want it to do when something happens, and then sit back and wait for the something to happen.

from your reply I believe thats not whats happening, because
otherwise I wouldn't need to use the GetEventObject to get the
object later..

[1]

So, my doubt is, what is that second argument being pass?

The object passed to SomeHandler is constructed at the time time that the event happens, just before the call to SomHandler is made. It is an instance of some class derived from wx.Event and it simply holds information about the event and where it came from. For example for a wx.EVT_KEY_DOWN you'll get a wx.KeyEvent that holds information about the key that was pressed, the current state of the modifier keys, etc. For a wx.EVT_SIZE you'll get an instance of wx.SizeEvent that can give you the new size of the window.

Try doing a "print event" in your handlers to see what you are getting.

[1] Just for the record you won't need to use GetEventObject most of the time, but it is helpful when you may be binding the same handler function for events that may be originating from multiple source objects. For example, if I do:

    self.Bind(wx.EVT_BUTTON, self.OnButton)

without telling it which button I want to get events from, then OnButton will be called for *any* button that is within the scope of (children, grandchildren of) the self window.