[wxPython-users] One more Event Question

Robin Dunn robin at alldunn.com
Mon Dec 3 12:10:19 PST 2007


Just a bit more clarification...


Stephen Hansen wrote:
> Mark Erbaugh wrote:
>     i.e. self.Bind(wx.EVT_BUTTON, self.OnClick, aButton)
> 
>     what objects are created?  I tried tracing the code, but it went to the
>     C code with a call to _core_.EvtHandler_Connect(*args, **kwargs) in
>     EvtHandler.Connect.
> 
> 
> ... why do you care what objects are created? :) 
> 
>     Do This objects need to be destroyed, presumbly with a call to UnBind?

No, it is all handled internally in wxWidgets.

> 
> 
> As a rule, you don't have to worry about any of that.
>  
> 
>     The Bind mechanism appears to allow binding of an event from one object
>     to a handler in another object.  I understand that this is needed to
>     bind an event handler for a button click to the associated frame. Would
>     it also be possible to bind an event handler from a totally separate
>     object?  

Yes.  Because of Python's dynamic nature wxPython has no limitations in 
this area.  *Any* callable object can be used as an event handler, it 
simply needs to accept one parameter for the event object.  The callable 
can be a function, a method of the target object, a method of some other 
object, or even an instance of some class that has a __call__ method.

> When the button and the event handler are both in the same
>     frame object, destroying the frame destroys the button as well. What
>     would happen if the event handler was in a separate object and that
>     object were destroyed, but the button wasn't?

The event binding belongs to the object on the left side of the ".Bind" 
so when that object is destroyed the binding is too.  So yes, in theory 
it's possible for the button to still exist but the binding will be 
gone, but in reality this will never be a problem because if destroying 
the frame does not also destroy the button, then that event binding 
would not have been receiving events anyway, because it is not in the 
containment hierarchy of the button.

[ some good explanations from Stephen clipped...]

You should also read http://wiki.wxpython.org/self.Bind_vs._self.button.Bind


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list