Tkinter Widget command function call

Grant Edwards ge at nowhere.none
Fri Jun 16 12:16:37 EDT 2000


In article <3949E71A.AF53A1CF at muc.das-werk.de>, Thomas Thiele wrote:

>I create some buttons dynamically in a for loop:
>
>for i in range(0, n):
>	MyButton = Button(frame, text="press me", command=self.CmButtonPressed) 
>	MyButton.pack(side=TOP)	
>
>And now I want to check witch button has called the CmButtonPressed -
>function.
>
>def CmButtonPressed(self, ???):
>	print "Button ", buttonnumber, "pressed"
>
>What is the best way to do this? Has anaboby an idea?

I'd use lambda to create the command functions on the fly, but
I'm an old Scheme geek so I think rather oddly about some
things.

Something like:

----------------------------------------------------------------------
from Tkinter import *

root = Tk()

def buttonPressed(n):
    print "button %d pressed" % n
    
for i in range(0,4):
    Button(root,
           text = "Button %d" % i,
           command = lambda j=i: buttonPressed(j)).pack()
           
root.mainloop()
----------------------------------------------------------------------



-- 
Grant Edwards                   grante             Yow!  Imagine--a WORLD
                                  at               without POODLES...
                               visi.com            



More information about the Python-list mailing list