Tkinter.Button(... command) lambda and argument problem

Jay jaysherby at gmail.com
Fri Sep 15 19:36:23 EDT 2006


I'm having a problem using lambda to use a command with an argument for
a button in Tkinter.

                buttons = range(5)
		for x in xrange(5):
			buttons[x] = Button(frame, text=str(x+1), command=lambda:
self.highlight(x))
			buttons[x].pack(side=LEFT)

The buttons are correctly numbered 1 through 5, but no matter which
button I click on, it sends the number 4 as an argument to the
highlight function.  How can I correct this?

By the way, I've tried changing xrange to range with no success.




More information about the Python-list mailing list