pythonpython-3.xtkinterwidget

How to get widget name in event?


from tkinter import *

main = Tk()

def flipper(event):
    # I'd like to do this:
    #if widgetname == switcher:
        #do stuff
    #if widgetname == switcher1:
        #do stuff
    return

switcher = Label(main, bg='white', text="click here", font="-weight bold")
switcher.grid()
switcher.bind("<Button-1>", flipper)


switcher1 = Label(main, bg='white', text="click here", font="-weight bold")
switcher1.grid()
switcher1.bind("<Button-1>", flipper)

switcher2 = Label(main, bg='white', text="click here", font="-weight bold")
switcher2.grid()
switcher2.bind("<Button-1>", flipper)

switcher3 = Label(main, bg='white', text="click here", font="-weight bold")
switcher3.grid()
switcher3.bind("<Button-1>", flipper)

switcher4 = Label(main, bg='white', text="click here", font="-weight bold")
switcher4.grid()
switcher4.bind("<Button-1>", flipper)

switcher5 = Label(main, bg='white', text="click here", font="-weight bold")
switcher5.grid()
switcher5.bind("<Button-1>", flipper)


main.mainloop()

In my event function I'd like to do different things based on the label that is clicked. What im stumped on is that I can only get the identifier number of the widget that is clicked, not the name. If I could get the identifier of all my widgets then I could do:

def flipper(event):
    if event.widget == switcher.identifier():
           do stuff

but I can't find how to get the id of a specified widget either...

How can I get the name of a widget by its identifier (event.widget())?

Or how can I get the identifier of a specified widget name?

If neither are possible, then I'd have to make a different function and bind for each label which is a lot of work that hopefully is not necessary.


Edit:

from tkinter import *

main = Tk()

def flipper(event, switch):
    if switch.widget == 's1':
        print("got it")

switcher = Label(main, bg='white', text="click here", font="-weight bold")
switcher.grid()
switcher.bind("<Button-1>", flipper)
switcher.widget = 's1'


main.mainloop()

Solution

  • You can't get the variable name that the widget is assigned to, that would be relatively useless. A widget could be assigned to more than one variable, or none at all.

    Getting the label text

    You have access to the actual widget, and you can use that to get the text that is on the label. Your example shows that all labels are the same, so this might not be useful to you:

    def flipper(event):
        print("label text:", event.widget.cget("text"))
    

    Using a custom widget name

    You can also give a widget a name. You can't get back precisely the name, but you can come very close. For example, if you create a label like this:

    switcher = Label(main, name="switcher", bg='white', text="click here", font="-weight bold")
    

    You can get the string representation of the widget by splitting on "." and taking the last value:

    def flipper(event):
        print("widget name:", str(event.widget).split(".")[-1])
    

    Passing a name via the binding

    Finally, you can set up your bindings such that the name is sent to the function:

    switcher.bind("<Button-1>", lambda event: flipper(event, "switcher"))
    switcher1.bind("<Button-1>", lambda event: flipper(event, "switcher1"))