[Tutor] abs beginner first project

Michael Lange klappnase at freenet.de
Wed Jul 12 19:58:28 CEST 2006


On Wed, 12 Jul 2006 06:48:44 -0500
Luke Paireepinart <rabidpoobear at gmail.com> wrote:

> For example, in Tkinter, you can bind buttons to function calls, but you 
> can't have buttons pass arguments to the functions.  So you have to 
> somehow make a separate function for each button, or do some weird 
> backend event handling stuff that you can look up  if you're really 
> interested.

BTW, that is not really true:

>>> from Tkinter import *
>>> def test(*args):
...     print args
... 
>>> root = Tk()
>>> b = Button(root, text='Hi', command=lambda widget=b: test(widget))
>>> b.pack()

Now when you press the button, you get:

>>> (<Tkinter.Button instance at 0xb78a478c>,)

>>>

so the callback actually "knows" which widget called it, and you could easily perform
different actions for several buttons.

I know that is not what this thread is about, I just could not resist...

Michael


More information about the Tutor mailing list