Get the ID of a widget that invoke an event in Tkinter


Tkinter provides a mechanism to bind events to widgets. When an event occurs, such as a button click or mouse movement, a specific function or method associated with that event is executed. The event handler function typically takes an event object as a parameter, which provides information about the event. While the event object does not directly include the ID of the widget, we can use various techniques to identify the widget based on the event. In this article, let’s explore these approaches.

Approach 1

Let's start by examining a simple example that demonstrates how to retrieve the ID of a widget that invokes an event −

Example

import tkinter as tk

def on_button_click(event):
   widget_id = event.widget.winfo_id()
   print(f"The ID of the widget is: {widget_id}")

root = tk.Tk()
root.geometry("720x250")
root.title("Retrieving the ID of a widget that invokes an event")

button = tk.Button(root, text="Click Me")
button.bind("<Button-1>", on_button_click)
button.pack()

root.mainloop()

In the code above, we import the tkinter module and create a basic Tkinter application with a root window. We define a function on_button_click to handle the button click event. The event object is passed as a parameter to this function.

Inside the event handler function, we retrieve the widget ID by accessing the widget attribute of the event object. We use the winfo_id() method on the widget to obtain its ID. Finally, we print the widget ID using an f-string.

Next, we create a Button widget named button and bind the left mouse button click event ("<Button-1>") to the on_button_click function. This means that whenever the button is clicked, the event handler function will be called.

Output

When you run the code, you will see a Tkinter window with a button. When you click the button, the ID of the widget (button) that triggered the event will be printed to the console.

The ID of the widget is: 1771520

Approach 2

Another approach is to assign a unique identifier to each widget and use it to identify the widget when an event occurs. Tkinter provides the tkinter.Variable class, which can be used to store data associated with widgets. We can create a variable for each widget and set a unique value to it. Later, we can access the variable value to retrieve the widget ID.

Example

Here's an example demonstrating this approach −

import tkinter as tk

def on_button_click():
   button_id = button_var.get()
   print(f"The ID of the button is: {button_id}")

root = tk.Tk()
root.geometry("720x250")
root.title("Retrieving the ID of a widget by assigning a unique identifier to each widget")

button_var = tk.StringVar(value="Button-1")
button = tk.Button(root, command=on_button_click)
button.config(textvariable=button_var)
button.pack()

root.mainloop()

In this code, we define a function on_button_click to handle the button click event. Inside the function, we retrieve the button ID by accessing the value of the button_var variable.

We create a StringVar object named button_var and assign a unique value to it. This value represents the ID of the button. Later, we configure the button widget to use this variable as its textvariable. This means that the displayed text of the button will be the value stored in the button_var.

Output

When the button is clicked, the on_button_click function is called, and the button ID is retrieved from the button_var. Finally, the widget ID is printed to the console.

The ID of the button is: Button-1

Approach 3

Another approach to obtain the ID of the widget that invoked an event is to use lambda functions or partial functions. These techniques allow us to pass additional arguments to the event handler function, such as the widget ID.

Example

Here's an example illustrating the use of lambda functions −

import tkinter as tk

def on_button_click(widget_id):
   print(f"The ID of the widget is: {widget_id}")

root = tk.Tk()
root.geometry("720x250")
root.title("Retrieving the ID of a widget by using lambda functions")

button1 = tk.Button(root, text="Button 1", command=lambda: on_button_click("Button-1"))
button1.pack()

button2 = tk.Button(root, text="Button 2", command=lambda: on_button_click("Button-2"))
button2.pack()
root.mainloop()

In this code, we define a function on_button_click to handle the button click event. It takes a parameter widget_id, which represents the ID of the widget.

We create two button widgets (button1 and button2). When defining the command for each button, we use lambda functions to call the on_button_click function with the corresponding widget ID as an argument.

Output

When you run the code, clicking on each button will invoke the on_button_click function with the respective widget ID. The widget ID is then printed to the console.

The ID of the widget is: Button-1
The ID of the widget is: Button-2

Conclusion

Retrieving the ID of a widget that invokes an event in Tkinter can be accomplished using different techniques. By accessing the winfo_id() method, using variables to store widget IDs, or utilizing lambda functions with additional arguments, you can obtain the necessary information to identify the widget that triggered the event. These approaches provide flexibility in handling events and enable you to implement specific actions or conditional logic based on the widget ID. Experiment with these techniques and choose the one that best suits your application's requirements.

Updated on: 04-Dec-2023

130 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements