Getting the sender widget's name in function (Tkinter)

tiissa tiissa at nonfree.fr
Tue Apr 26 16:19:12 EDT 2005


Harlin Seritt wrote:
> I have the following script. Two widgets call the same function. How
> can I tell inside of the called function which button called it?:

As far as I know you can't (but I can be proven wrong).
You may try to define a class to solve this (not tested):

####
class say_hello:
     def __init__(self, text):
         self.text=text
     def __call__(self)
         print 'Hello!'
         print self.text

root = Tk()
button1 = Button(root, text='Button 1', command=say_hello('Button 1'))
button1.pack()
button2 = Button(root, text='Button 2', command=say_hello('Button 2'))
button2.pack()
root.mainloop()
####



More information about the Python-list mailing list