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

Cameron Laird Cameron at Phaseit.net
Sat Sep 25 18:36:52 CEST 2004


On Sat, Sep 25, 2004 at 10:17:58AM -0600, stewart at midtoad.homelinux.org wrote:
			.
			.
			.
> > 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).
			.
			.
			.
I think you're thinking of this in terms of syntactic scope.  While
that's a healthy instinct, the approach I find most successful in
explaining Tkinter's event orientation is to teach developers to 
categorize variables in terms of "define-time" (or "bind-time") vs.
"run-time" ("execute-time" or "use-time" or ...).  IT CAN EASILY 
HAPPEN THAT ONE BINDING INCLUDES BOTH.  I'm too weary now to write
this up for the Wiki.  I invite you to do so, though.


More information about the Tkinter-discuss mailing list