Pass parameters to EVT callback function

Hi,

How do I pass parameters to a callback function called by the EVT
macros?

Tks.

AL

Hi

···

On Sat, Nov 30, 2002 at 08:17:47PM +0800, Alfredo P. Ricafort wrote:

How do I pass parameters to a callback function called by the EVT
macros?

With tkInter theres a way to create a class holgind the function:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66521

This should work for wxwindows too.

Mirko
--
Mirko Koenig VIT Proz. http://www.v-i-t.de

homepage: http://koenig.v-i-t.de
ICQ: #11181751
e-mail: koenig@v-i-t.de

Without chaos, there is no need to be orderly

One simple way to is to the key arguments trick to create a function
at runtime. Example:

  def assign_callback ():
    my_var = 3
    def callback (event, var=my_var):
      print var
    EVT_BLAH (widget, callback)

  That might also help.

                  -- Mike

···

On Sat, Nov 30 @ 20:17, Alfredo P. Ricafort wrote:

Hi,

How do I pass parameters to a callback function called by the EVT
macros?

Tks.

AL

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

--
Michael Gilfix
mgilfix@eecs.tufts.edu

For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html

Depending on your level of Python, there is yet another method which may
(or may not) be cleaner....

class Callback:

  def __init__(self, func, *args, **kwargs):
    self.func = func
    self.args = args
    self.kwargs = kwargs

  def __call__(self):
    self.func(*args, **kwargs)

Thus, you can do the following

EVT_BLAH(widget, Callback(the_real_func, arg1, kwarg1 = value, ...))

and so forth.

I have used this in order to catch exceptions in callbacks and show the
results in a message box rather than let them appear in the stderr
window or on the command line. It works great... :slight_smile:

···

On Sat, 2002-11-30 at 10:10, Michael Gilfix wrote:

  One simple way to is to the key arguments trick to create a function
at runtime. Example:

  def assign_callback ():
    my_var = 3
    def callback (event, var=my_var):
      print var
    EVT_BLAH (widget, callback)

  That might also help.

                  -- Mike

On Sat, Nov 30 @ 20:17, Alfredo P. Ricafort wrote:
> Hi,
>
> How do I pass parameters to a callback function called by the EVT
> macros?
>
> Tks.
>
> AL
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
> For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
>

--
Anthony Tuininga <anthony@computronix.com>

This doesn't seems to work. The Callback is being called even before
the menu item receives the event, and the __call__ is not being called
when the menu item receives the event.

···

On Sun, 2002-12-01 at 05:51, Anthony Tuininga wrote:

Depending on your level of Python, there is yet another method which may
(or may not) be cleaner....

class Callback:

  def __init__(self, func, *args, **kwargs):
    self.func = func
    self.args = args
    self.kwargs = kwargs

  def __call__(self):
    self.func(*args, **kwargs)

Thus, you can do the following

EVT_BLAH(widget, Callback(the_real_func, arg1, kwarg1 = value, ...))

and so forth.

I have used this in order to catch exceptions in callbacks and show the
results in a message box rather than let them appear in the stderr
window or on the command line. It works great... :slight_smile:

On Sat, 2002-11-30 at 10:10, Michael Gilfix wrote:
> One simple way to is to the key arguments trick to create a function
> at runtime. Example:
>
> def assign_callback ():
> my_var = 3
> def callback (event, var=my_var):
> print var
> EVT_BLAH (widget, callback)
>
> That might also help.
>
> -- Mike
>
> On Sat, Nov 30 @ 20:17, Alfredo P. Ricafort wrote:
> > Hi,
> >
> > How do I pass parameters to a callback function called by the EVT
> > macros?
> >
> > Tks.
> >
> > AL
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
> > For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
> >

--
Alfredo P. Ricafort <alpot@mylinuxsite.com>
Home

Well, this is a cut and paste of code that actually works. The other
code was a bit of an abstraction so perhaps I mistyped something.

The way this works is that each control can have a "base handler" which
will handle the event and return a list of arguments which will then be
passed to the "user handler". I have used this to translate indexes into
useful keys in a base control class rather than have to do this over and
over again in each window. That said, this code works and it is short
enough you should be able to understand it without too much trouble -- I
don't think I'm that obtuse! :slight_smile: If you do need further explanation, let
me know and I'll see what I can do to help.

···

#------------------------------------------------------------------------------
# EventHandler()
# Event handling class.
#------------------------------------------------------------------------------
class EventHandler:

  def __init__(self, a_Control, a_Event, a_UserHandler,
      a_BaseHandler = None, a_Id = None):
    self.i_Control = a_Control
    self.i_UserHandler = a_UserHandler
    self.i_BaseHandler = a_BaseHandler
    if a_Id is None:
      a_Id = a_Control.GetId()
    a_Control.Connect(a_Id, -1, a_Event, self)

  def __call__(self, a_Event):
    try:
      v_Args = ()
      if self.i_BaseHandler:
        v_Args = self.i_BaseHandler(a_Event)
      if v_Args is not None:
        self.i_UserHandler(*v_Args)
      a_Event.Skip()
    except:
      MessageBoxWithTraceback(self.i_Control)

On Wed, 2002-12-04 at 08:47, Alfredo P. Ricafort wrote:

This doesn't seems to work. The Callback is being called even before
the menu item receives the event, and the __call__ is not being called
when the menu item receives the event.

On Sun, 2002-12-01 at 05:51, Anthony Tuininga wrote:
> Depending on your level of Python, there is yet another method which may
> (or may not) be cleaner....
>
> class Callback:
>
> def __init__(self, func, *args, **kwargs):
> self.func = func
> self.args = args
> self.kwargs = kwargs
>
> def __call__(self):
> self.func(*args, **kwargs)
>
> Thus, you can do the following
>
> EVT_BLAH(widget, Callback(the_real_func, arg1, kwarg1 = value, ...))
>
> and so forth.
>
> I have used this in order to catch exceptions in callbacks and show the
> results in a message box rather than let them appear in the stderr
> window or on the command line. It works great... :slight_smile:
>
> On Sat, 2002-11-30 at 10:10, Michael Gilfix wrote:
> > One simple way to is to the key arguments trick to create a function
> > at runtime. Example:
> >
> > def assign_callback ():
> > my_var = 3
> > def callback (event, var=my_var):
> > print var
> > EVT_BLAH (widget, callback)
> >
> > That might also help.
> >
> > -- Mike
> >
> > On Sat, Nov 30 @ 20:17, Alfredo P. Ricafort wrote:
> > > Hi,
> > >
> > > How do I pass parameters to a callback function called by the EVT
> > > macros?
> > >
> > > Tks.
> > >
> > > AL
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
> > > For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
> > >
--
Alfredo P. Ricafort <alpot@mylinuxsite.com>
Home

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

--
Anthony Tuininga
anthony@computronix.com

Computronix
Distinctive Software. Real People.
Suite 200, 10216 - 124 Street NW
Edmonton, AB, Canada T5N 4A3
Phone: (780) 454-3700
Fax: (780) 454-3838

Hi Anthony,

It DOES work!

I mistakenly passed the wrong parent class to the EVT_ macro. So my
mistake.

I'm sorry for taking up your time.

Thanks again.

AL

···

On Thu, 2002-12-05 at 00:07, Anthony Tuininga wrote:

Well, this is a cut and paste of code that actually works. The other
code was a bit of an abstraction so perhaps I mistyped something.

The way this works is that each control can have a "base handler" which
will handle the event and return a list of arguments which will then be
passed to the "user handler". I have used this to translate indexes into
useful keys in a base control class rather than have to do this over and
over again in each window. That said, this code works and it is short
enough you should be able to understand it without too much trouble -- I
don't think I'm that obtuse! :slight_smile: If you do need further explanation, let
me know and I'll see what I can do to help.

#------------------------------------------------------------------------------
# EventHandler()
# Event handling class.
#------------------------------------------------------------------------------
class EventHandler:

  def __init__(self, a_Control, a_Event, a_UserHandler,
      a_BaseHandler = None, a_Id = None):
    self.i_Control = a_Control
    self.i_UserHandler = a_UserHandler
    self.i_BaseHandler = a_BaseHandler
    if a_Id is None:
      a_Id = a_Control.GetId()
    a_Control.Connect(a_Id, -1, a_Event, self)

  def __call__(self, a_Event):
    try:
      v_Args = ()
      if self.i_BaseHandler:
        v_Args = self.i_BaseHandler(a_Event)
      if v_Args is not None:
        self.i_UserHandler(*v_Args)
      a_Event.Skip()
    except:
      MessageBoxWithTraceback(self.i_Control)

On Wed, 2002-12-04 at 08:47, Alfredo P. Ricafort wrote:
> This doesn't seems to work. The Callback is being called even before
> the menu item receives the event, and the __call__ is not being called
> when the menu item receives the event.
>
>
> On Sun, 2002-12-01 at 05:51, Anthony Tuininga wrote:
> > Depending on your level of Python, there is yet another method which may
> > (or may not) be cleaner....
> >
> > class Callback:
> >
> > def __init__(self, func, *args, **kwargs):
> > self.func = func
> > self.args = args
> > self.kwargs = kwargs
> >
> > def __call__(self):
> > self.func(*args, **kwargs)
> >
> > Thus, you can do the following
> >
> > EVT_BLAH(widget, Callback(the_real_func, arg1, kwarg1 = value, ...))
> >
> > and so forth.
> >
> > I have used this in order to catch exceptions in callbacks and show the
> > results in a message box rather than let them appear in the stderr
> > window or on the command line. It works great... :slight_smile:
> >
> > On Sat, 2002-11-30 at 10:10, Michael Gilfix wrote:
> > > One simple way to is to the key arguments trick to create a function
> > > at runtime. Example:
> > >
> > > def assign_callback ():
> > > my_var = 3
> > > def callback (event, var=my_var):
> > > print var
> > > EVT_BLAH (widget, callback)
> > >
> > > That might also help.
> > >
> > > -- Mike
> > >
> > > On Sat, Nov 30 @ 20:17, Alfredo P. Ricafort wrote:
> > > > Hi,
> > > >
> > > > How do I pass parameters to a callback function called by the EVT
> > > > macros?
> > > >
> > > > Tks.
> > > >
> > > > AL
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
> > > > For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
> > > >
> --
> Alfredo P. Ricafort <alpot@mylinuxsite.com>
> Home
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
> For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

--
Alfredo P. Ricafort <alpot@mylinuxsite.com>
Home

Hi Anthony,

It DOES work!

Glad to hear it.

I mistakenly passed the wrong parent class to the EVT_ macro. So my
mistake.

I'm sorry for taking up your time.

No problem. I'd like others to be patient with me, too so it behooves me
to be patient with others.... :slight_smile:

···

On Wed, 2002-12-04 at 09:28, Alfredo P. Ricafort wrote:

Thanks again.

AL

On Thu, 2002-12-05 at 00:07, Anthony Tuininga wrote:
> Well, this is a cut and paste of code that actually works. The other
> code was a bit of an abstraction so perhaps I mistyped something.
>
> The way this works is that each control can have a "base handler" which
> will handle the event and return a list of arguments which will then be
> passed to the "user handler". I have used this to translate indexes into
> useful keys in a base control class rather than have to do this over and
> over again in each window. That said, this code works and it is short
> enough you should be able to understand it without too much trouble -- I
> don't think I'm that obtuse! :slight_smile: If you do need further explanation, let
> me know and I'll see what I can do to help.
>
> #------------------------------------------------------------------------------
> # EventHandler()
> # Event handling class.
> #------------------------------------------------------------------------------
> class EventHandler:
>
> def __init__(self, a_Control, a_Event, a_UserHandler,
> a_BaseHandler = None, a_Id = None):
> self.i_Control = a_Control
> self.i_UserHandler = a_UserHandler
> self.i_BaseHandler = a_BaseHandler
> if a_Id is None:
> a_Id = a_Control.GetId()
> a_Control.Connect(a_Id, -1, a_Event, self)
>
> def __call__(self, a_Event):
> try:
> v_Args = ()
> if self.i_BaseHandler:
> v_Args = self.i_BaseHandler(a_Event)
> if v_Args is not None:
> self.i_UserHandler(*v_Args)
> a_Event.Skip()
> except:
> MessageBoxWithTraceback(self.i_Control)
>
> On Wed, 2002-12-04 at 08:47, Alfredo P. Ricafort wrote:
> > This doesn't seems to work. The Callback is being called even before
> > the menu item receives the event, and the __call__ is not being called
> > when the menu item receives the event.
> >
> >
> > On Sun, 2002-12-01 at 05:51, Anthony Tuininga wrote:
> > > Depending on your level of Python, there is yet another method which may
> > > (or may not) be cleaner....
> > >
> > > class Callback:
> > >
> > > def __init__(self, func, *args, **kwargs):
> > > self.func = func
> > > self.args = args
> > > self.kwargs = kwargs
> > >
> > > def __call__(self):
> > > self.func(*args, **kwargs)
> > >
> > > Thus, you can do the following
> > >
> > > EVT_BLAH(widget, Callback(the_real_func, arg1, kwarg1 = value, ...))
> > >
> > > and so forth.
> > >
> > > I have used this in order to catch exceptions in callbacks and show the
> > > results in a message box rather than let them appear in the stderr
> > > window or on the command line. It works great... :slight_smile:
> > >
> > > On Sat, 2002-11-30 at 10:10, Michael Gilfix wrote:
> > > > One simple way to is to the key arguments trick to create a function
> > > > at runtime. Example:
> > > >
> > > > def assign_callback ():
> > > > my_var = 3
> > > > def callback (event, var=my_var):
> > > > print var
> > > > EVT_BLAH (widget, callback)
> > > >
> > > > That might also help.
> > > >
> > > > -- Mike
> > > >
> > > > On Sat, Nov 30 @ 20:17, Alfredo P. Ricafort wrote:
> > > > > Hi,
> > > > >
> > > > > How do I pass parameters to a callback function called by the EVT
> > > > > macros?
> > > > >
> > > > > Tks.
> > > > >
> > > > > AL
> > > > >
> > > > > ---------------------------------------------------------------------
> > > > > To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
> > > > > For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
> > > > >
> > --
> > Alfredo P. Ricafort <alpot@mylinuxsite.com>
> > Home
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
> > For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
--
Alfredo P. Ricafort <alpot@mylinuxsite.com>
Home

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

--
Anthony Tuininga
anthony@computronix.com

Computronix
Distinctive Software. Real People.
Suite 200, 10216 - 124 Street NW
Edmonton, AB, Canada T5N 4A3
Phone: (780) 454-3700
Fax: (780) 454-3838