[Tkinter-discuss] Re: passing parameters to a lambda function inTkinter menubar

stewart at midtoad.homelinux.org stewart at midtoad.homelinux.org
Sat Sep 25 18:17:58 CEST 2004


Quoting Fredrik Lundh <fredrik at pythonware.com>:


> the lambda isn't part of the class, it's a function object.  to be able to
> find the right self, bind it just like you bind the size value.

doh!  That seems so obvious when you explain it.  Naturally, though, that
concept doesn't appear in any documentation I've read about lambda functions.

> according to the "functions are cheap, lambdas are hard to read" rule,
> this is better written as:
> 
>             def setfontsize(self=self, size=size):
>                 self.setFont(size)
>             menuBar.addmenuitem('Size', 'command', 'Set size to ' + size,
>                      command = setfontsize, font=self.font, label = size)

This is great! It allows the code to be read more easily, while still allowing
me to pass arguments through the callback.  This great little tip should be on
the tkinter wiki site (note to self).


thanks again
Stewart



More information about the Tkinter-discuss mailing list