Callbacks to classes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Callbacks to classes

bvdp
New to list ... hope it's not been covered before, and on topic ....

Anyway, I've got a number of programs which suddenly stopped working.
I think due to a change from python 2.6 to 2.7.

I was setting up a menu call back to a Class with code like:

    Button(bf, text=txt, height=1, command=cmd).grid(column=c, row=0, pady=5)

when 'cmd' was a simple class name. Doesn't work anymore :)

However, if I change cmd to a lambda it's find.

Sorry, this isn't a better (simple) example ... I'm pulling code out a
non-trivial application I've been running for several years. But, most
simply I'd have something like:

 Class foo:
   ... init, and present a options window, etc.
   --- set a call back to delete the window when done.

Setting 'cmd' to 'foo' doesn't work anymore. However,

 cmd = lambda:foo()

solves the issue.

I'm thinking I've been doing it wrong all along?

Best,

--
**** Listen to my CD at http://www.mellowood.ca/music/cedars ****
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: [hidden email]
WWW:   http://www.mellowood.ca
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Callbacks to classes

bvdp
> Anyway, I've got a number of programs which suddenly stopped working.
> I think due to a change from python 2.6 to 2.7.
>
> I was setting up a menu call back to a Class with code like:
>
>    Button(bf, text=txt, height=1, command=cmd).grid(column=c, row=0, pady=5)
>
> when 'cmd' was a simple class name. Doesn't work anymore :)
>
> However, if I change cmd to a lambda it's find.

I got some help over in comp.python on this. Apparently there was a
change between 2.6 and 2.7. There are 3 easy solutions:

1. Use a function :)
2. Use lambda.
3. Use a new style class, ie:

      class mywindow(object)

I've tried all three and they all work. I'm not sure what the best method is.

Best to all.

--
**** Listen to my CD at http://www.mellowood.ca/music/cedars ****
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: [hidden email]
WWW:   http://www.mellowood.ca
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Callbacks to classes

Alan Gauld

"Bob van der Poel" <[hidden email]> wrote

> > I was setting up a menu call back to a Class with code like:
> >
> > Button(bf, text=txt, height=1, command=cmd).grid(column=c, row=0,
> > pady=5)
> I got some help over in comp.python on this. Apparently there was a
> change between 2.6 and 2.7. There are 3 easy solutions:
>
> 1. Use a function :)
> 2. Use lambda.
> 3. Use a new style class, ie:
> I've tried all three and they all work. I'm not sure what the best
> method is.

Using a class is very unusual and has several drawbacks.
The main one is that Tkinter calls the call-back object which
in the case of a class creates a new instance. So you are
constantly creating new objects to which you have no
reference so cannot call their methods - which is a
bit pointless! So any action you perform needs to be in
the init method. You can do more with class callbacks
if you store the objects somewhere, like a class variable
or global list but its all very unusual and only used for
special occasions such as rewinding history etc..

Using a functiion is good if you have the same code shared
by many events or if it takes parameters, although in that
case you also need...

A lambda is probably the most common, especially when
you really want to call another function. The lambda body
just executes the function passing whatever parameters
are required.

So I'd say the normal case would be a combination
of lambda and function and classes used very rarely,
more or less only when persistent actions are required.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Callbacks to classes

Gregory Ewing
Alan Gauld wrote:

> Using a class is very unusual and has several drawbacks.
> The main one is that Tkinter calls the call-back object which
> in the case of a class creates a new instance. So you are
> constantly creating new objects to which you have no
> reference so cannot call their methods

It's not useless if, as appears to be the case here,
the class in question is implementing a modal dialog.
On the contrary, I think it's actually rather elegant,
if a bit surprising!

--
Greg
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Callbacks to classes

bvdp
> It's not useless if, as appears to be the case here,
> the class in question is implementing a modal dialog.
> On the contrary, I think it's actually rather elegant,
> if a bit surprising!

Thanks for confirming that I'm not completely out of my mind doing
things this way :)

Seriously, I'm sure it's not my idea! I just don't recall where I got
the idea from. But, if you are interested in seeing the program it's
available at:

 http://mellowood.ca/xpmidi/index.html

Best,

--
**** Listen to my CD at http://www.mellowood.ca/music/cedars ****
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: [hidden email]
WWW:   http://www.mellowood.ca
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss