[Tutor] Recursive Tkinter buttons

Alan Gauld alan.gauld at freenet.co.uk
Sun Feb 27 17:06:32 CET 2005


>  - 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



More information about the Tutor mailing list