Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the widget name in the event in Tkinter?
Tkinter is a Python library used to create GUI-based applications. When working with multiple widgets and event handling, you often need to identify which specific widget triggered an event. This can be achieved using the event.widget object and its properties.
Getting Widget Name Using event.widget
The most direct way to get a widget's name in an event is by accessing event.widget in your event handler function. Here's how to get different types of widget identification ?
import tkinter as tk
def on_button_click(event):
# Get the widget that triggered the event
widget = event.widget
# Get widget text (for buttons, labels)
if hasattr(widget, 'cget') and 'text' in widget.keys():
print(f"Widget text: {widget['text']}")
# Get widget class name
print(f"Widget type: {widget.winfo_class()}")
# Get widget name
print(f"Widget name: {widget.winfo_name()}")
# Create main window
root = tk.Tk()
root.geometry("400x200")
# Create buttons with different text
btn1 = tk.Button(root, text="Button 1")
btn1.bind("<Button-1>", on_button_click)
btn1.pack(pady=10)
btn2 = tk.Button(root, text="Button 2")
btn2.bind("<Button-1>", on_button_click)
btn2.pack(pady=10)
root.mainloop()
Getting Widget Text Specifically
If you want to get the text displayed on widgets like buttons or labels, use event.widget["text"] ?
import tkinter as tk
def show_widget_text(event):
widget_text = event.widget["text"]
print(f"Clicked widget: {widget_text}")
root = tk.Tk()
root.geometry("300x150")
# Create multiple buttons
buttons = ["Save", "Load", "Delete", "Exit"]
for text in buttons:
btn = tk.Button(root, text=text)
btn.bind("<Button-1>", show_widget_text)
btn.pack(pady=5)
root.mainloop()
Clicked widget: Save Clicked widget: Load Clicked widget: Delete
Getting All Widget Children
You can also get a list of all child widgets in a window using winfo_children() ?
import tkinter as tk
def show_all_widgets():
children = root.winfo_children()
print("All widgets in window:")
for widget in children:
print(f"- {widget.winfo_class()}: {widget}")
root = tk.Tk()
root.geometry("400x200")
# Create various widgets
canvas = tk.Canvas(root, width=300, height=100, bg="lightblue")
canvas.pack(pady=10)
btn = tk.Button(root, text="Show Widgets", command=show_all_widgets)
btn.pack()
label = tk.Label(root, text="Sample Label")
label.pack()
root.mainloop()
All widgets in window: - Canvas: .!canvas - Button: .!button - Label: .!label
Practical Example with Widget Identification
Here's a practical example showing how to identify widgets in a calculator-like interface ?
import tkinter as tk
def button_clicked(event):
button_text = event.widget["text"]
print(f"Button '{button_text}' was clicked")
if button_text.isdigit():
print("This is a number button")
elif button_text in ["+", "-", "*", "/"]:
print("This is an operator button")
elif button_text == "=":
print("This is the equals button")
root = tk.Tk()
root.title("Calculator Demo")
root.geometry("250x200")
# Create calculator buttons
buttons = [
["7", "8", "9", "+"],
["4", "5", "6", "-"],
["1", "2", "3", "*"],
["0", "=", "/", "C"]
]
for row in buttons:
frame = tk.Frame(root)
frame.pack()
for btn_text in row:
btn = tk.Button(frame, text=btn_text, width=4, height=2)
btn.bind("<Button-1>", button_clicked)
btn.pack(side=tk.LEFT, padx=2, pady=2)
root.mainloop()
Conclusion
Use event.widget["text"] to get widget text, event.widget.winfo_class() for widget type, and winfo_children() to list all widgets. This enables dynamic event handling based on which widget triggered the event.
