Button (command=funtion) in Tkinter: pasing arguments

Phlip phlip_cpp at yahoo.com
Fri Aug 30 06:12:40 CEST 2002

revyakin wrote:

> If I write :
> ...command = displayCoords(canvas.gettags(currentButton))

With Python22 or higher you can exploit closures:

        def displayCoords(event):
                coords = genericButton.coords()
                print coords

        genericButton.config(command = displayCoords)   

The rest of your code (snipped) contains baggage such as a canvas that you 
may need, but not for this example.

Observe that because 'displayCoords' exists in the same scope as 
'genericButton', it essentially gets passed in as a "magic local variable". 
For some reason the computing industry calls this a "closure".

If you use Python < 2.2 complain, and we'l show the slightly more odious 
ways to do this.

  --  Have a :-) day  --

More information about the Python-list mailing list