When you have many widgets calling the same function, or triggering the same event in Tkinter, you often need a way to check which particular widget triggered/called it. There are two different ways functions and events are called in Tkinter.
In this Tkinter tutorial we will discuss how to handle both techniques, and to correctly find the type of the widget!
Check which Widget triggered the Event
Here is our very first example, which features a Label that has been binded to the Button-1 event (Left-mouse click). This means that whenever the Label is clicked (with Left click), the onClick
function that we have defined will run.
import tkinter as tk
def onClick(event):
print("Function was triggered by: ", event.widget)
root = tk.Tk()
label = tk.Label(root, text="Click me")
label.pack(padx = 20, pady = 20)
label.bind('<Button-1>', onClick)
root.mainloop()
By default, any function called by binding an event to a widget with the bind() method will have an “event” parameter passed to it. Accessing the .widget
attribute gives you the following output.
Function was triggered by: .!label
By default when ever you print out a tkinter widget, you get it’s “hierarchy”. For example, the above output tells us that the label is located directly within “root” (where the ‘.!’ are used to separate widgets).
If the Label was in a frame that was in root, then we would get this: .!frame.!label
.
For a more readable option, you can use the winfo_class() to check the type of the Widget.
def onClick(event):
print("Function was triggered by: ", event.widget.winfo_class())
Function was triggered by: Label
You can also make direct comparisons as shown below to develop interesting conditional logic, based on which widget called the function.
def onClick(event):
print("Function was triggered by: ", event.widget)
if label == event.widget:
print("Triggered by Label widget")
“label” in the above code is the name of the label widget we created earlier.
Find Widget Type when using command()
When using the command parameter, there are some widgets that do not pass any parameters, such as the Button widget. For them, we need to resort to a slightly different technique as shown below.
In the below code we have passed the button itself as a parameter to a lambda function, which then calls the onClick
function (obj is the parameter we passed to the lambda function).
import tkinter as tk
def onClick(widget):
print("Type:", widget.winfo_class())
root = tk.Tk()
button = tk.Button(root, text = "Button 1")
button.config(command = lambda obj=button : onClick(obj))
button.pack(padx = 20, pady = 20)
root.mainloop()
Type: Button
Here is another interesting example combining several of the concepts we have discussed in this tutorial. Try running the code yourself to observe what the program does!
import tkinter as tk
def onClick(widget):
if widget == button1:
l1.config(text = "Triggered by Button 1")
elif widget == button2:
l1.config(text = "Triggered by Button 2")
root = tk.Tk()
l1 = tk.Label(root, text="Click me")
l1.pack(padx = 20, pady = 20)
l1.bind('<Button-1>', onClick)
button1 = tk.Button(root, text = "Button 1")
button1.config(command= lambda obj=button1 : onClick(obj))
button1.pack(padx = 20, pady = 20)
button2 = tk.Button(root, text = "Button 2")
button2.config(command= lambda obj=button2 : onClick(obj))
button2.pack(padx = 20, pady = 20)
root.mainloop()

This marks the end of the How to check which widget called a function (event) in Tkinter? Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the Tutorial content can be asked in the comments section below.