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.

Using winfo_id() Method

The most direct approach is to use the winfo_id() method on the widget object from the event parameter

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 unique ID. Finally, we print the widget ID using an f-string.

The ID of the widget is: 1771520

Using Custom Widget Identifiers

Another approach is to assign a unique identifier to each widget and use it to identify the widget when an event occurs. We can create a variable for each widget and set a unique value to it

Example

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")

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 create a StringVar object named button_var and assign a unique value to it. This value represents the custom ID of the button. We configure the button widget to use this variable as its textvariable, so the displayed text shows our custom ID.

The ID of the button is: Button-1

Using Lambda Functions with Custom IDs

We can use lambda functions to pass additional arguments to the event handler function, such as custom widget identifiers

Example

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 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 approach, we create two button widgets with different custom IDs. 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.

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

Comparison

Method ID Type Best For
winfo_id() System-generated number Debugging, system-level identification
Custom Variables User-defined string Meaningful widget identification
Lambda Functions User-defined string Multiple widgets with custom logic

Conclusion

Use winfo_id() for system-level widget identification, custom variables for meaningful IDs, or lambda functions when you need to pass custom identifiers to multiple widgets. Choose the approach that best fits your application's requirements.

Updated on: 2026-04-02T17:21:25+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements