Determine which Button was pressed in Tkinter

In Tkinter applications, you often need to identify which button was clicked to perform specific actions. You can achieve this using callback functions with parameters that identify each button.

Using Lambda Functions with Parameters

The most common approach is to use lambda functions that pass identifying information to a callback function ?

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of Tkinter Frame
win = Tk()

# Set the geometry
win.geometry("700x250")

# Define function to get the information about the Button
def get_button(button_info):
    print(f"You clicked: {button_info}")

# Create Button Objects
b1 = ttk.Button(win, text="Button-1", command=lambda: get_button("Button-1"))
b1.place(relx=0.46, rely=0.5, anchor=CENTER)

b2 = ttk.Button(win, text="Button-2", command=lambda: get_button("Button-2"))
b2.place(relx=0.58, rely=0.5, anchor=CENTER)

win.mainloop()

The output when clicking "Button-1" will be ?

You clicked: Button-1

Alternative Method Using Button Text

You can also identify buttons by accessing their text property directly ?

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("700x250")

def button_clicked(button_widget):
    button_text = button_widget.cget("text")
    print(f"Button pressed: {button_text}")

# Create buttons and pass the button object itself
b1 = ttk.Button(win, text="Home")
b1.configure(command=lambda: button_clicked(b1))
b1.place(relx=0.4, rely=0.5, anchor=CENTER)

b2 = ttk.Button(win, text="Settings")
b2.configure(command=lambda: button_clicked(b2))
b2.place(relx=0.6, rely=0.5, anchor=CENTER)

win.mainloop()

This approach is useful when you want to use the button's actual properties rather than hardcoded strings.

Using a Dictionary for Multiple Actions

For more complex scenarios, you can map buttons to specific actions using a dictionary ?

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("700x250")

def handle_action(action):
    actions = {
        "save": "File saved successfully!",
        "open": "File dialog opened!",
        "exit": "Application closing..."
    }
    print(actions.get(action, "Unknown action"))

# Create buttons with specific actions
save_btn = ttk.Button(win, text="Save", command=lambda: handle_action("save"))
save_btn.place(relx=0.3, rely=0.5, anchor=CENTER)

open_btn = ttk.Button(win, text="Open", command=lambda: handle_action("open"))
open_btn.place(relx=0.5, rely=0.5, anchor=CENTER)

exit_btn = ttk.Button(win, text="Exit", command=lambda: handle_action("exit"))
exit_btn.place(relx=0.7, rely=0.5, anchor=CENTER)

win.mainloop()

Conclusion

Use lambda functions with parameters to identify which button was pressed in Tkinter. This approach allows you to create flexible callback systems that can handle multiple buttons with a single function.

Updated on: 2026-03-25T20:45:03+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements