[Tutor] Recursive Tkinter buttons

Adam Cripps kabads at gmail.com
Sun Feb 27 20:32:27 CET 2005


On Sun, 27 Feb 2005 16:06:32 -0000, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
> >  - however, when I click the button, I want self.showButton to know
> > which one of them was pressed. I've seen in other gui programming
> the
> > idea of an id or identifier - I can't see that here. Ideally, I
> would
> > like to know the value of i in self.showButtons - but when I use
> > self.showButtons(i) showButtons gets called straight at run time.
> 
> The easy way to do this is to use a defrault parameter in a lambda:
> 
> submittext = ">>> "
> for i in range (1, 11):
>    b = Button(text=submittext, command = lambda n=i:
> self.showButton(n))
>    b.rid(column=4, row=i+4)
>    submitlist.append(b)
> 
> Notice the button creation line now uses a lambda with default
> parameter set to i. That lambda calls your method but now passes
> in the value of i for that button. (You need to modify your method
> to accept that value of course!)
> 
> HTH,
> 
> Alan G
> Author of the Learn to Program web tutor
> http://www.freenetpages.co.uk/hp/alan.gauld

Yay! Thanks for the tips Michael/Alan - works a treat, although I must
admit, I'm not sure what Lambda does.

Adam

-- 
http://www.monkeez.org
PGP key: 0x7111B833


More information about the Tutor mailing list