Open In App

How to check which Button was clicked in Tkinter ?

Last Updated : 25 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Are you using various buttons in your app, and you are being confused about which button is being pressed? Don’t know how to get rid of this solution!! Don’t worry, just go through the article. In this article, we will explain in detail the procedure to know which button was pressed.

Steps by Step Approach:

Step 1: First, import the library Tkinter.

from tkinter import *

Step 2: Now, create a GUI app using Tkinter.

app = Tk()

Step 3: Then, create a function with one parameter, i.e., of the text you want to show when a button is clicked

def which_button(button_press):
   print (button_press)

Step 4: Further, create and display the first button by calling the which_button function you declared in step 3.

b1 = Button(app, text="#Text you want to show in button b1",
            command=lambda m="#Text you want to show when\
            b1 is clicked": which_button(m))
            
b1.grid(padx=10, pady=10)

Step 5: Moreover, create and display the second button by calling the which_button function you declared in step 3.

b2 = Button(app, text="#Text you want to show in button b2",
            command=lambda m="#Text you want to show when \
            b2 is clicked": which_button(m))
            
b2.grid(padx=10, pady=10)

Step 6: Next, keep on repeating steps 4 and 5 for n number of buttons by replacing n with the number of buttons you want it to display on the app. Don’t forget to call the which_button function you declared in step 3.

bn = Button(app, text="#Text you want to show in button bn",
            command=lambda m="#Text you want to show when \
            bn is clicked": which_button(m))
            
bn.grid(padx=10, pady=10)

Step 7: Finally, create an infinite loop for displaying the app on the screen.

app.mainloop()

Example: 

In this example, if the text ‘It is an apple‘ is printed on the screen, we get to know ‘Apple‘ button is pressed, else when ‘It is a banana‘ is printed on the screen, we get to know ‘Banana‘ button is pressed.

Python




# Python program to determine which
# button was pressed in tkinter
 
# Import the library tkinter
from tkinter import *
 
# Create a GUI app
app = Tk()
 
# Create a function with one parameter, i.e., of
# the text you want to show when button is clicked
def which_button(button_press):
    # Printing the text when a button is clicked
    print(button_press)
 
 
# Creating and displaying of button b1
b1 = Button(app, text="Apple",
            command=lambda m="It is an apple": which_button(m))
 
b1.grid(padx=10, pady=10)
 
# Creating and displaying of button b2
b2 = Button(app, text="Banana",
            command=lambda m="It is a banana": which_button(m))
b2.grid(padx=10, pady=10)
 
# Make the infinite loop for displaying the app
app.mainloop()


Output:


Previous Article
Next Article

Similar Reads

Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
PyQt5 QCalendarWidget - Clicked signal
In this article we will see how we can get the clicked signal from the QCalendarWidget. Clicked signal is emitted when a mouse button is clicked i.e when the mouse was clicked on the specified date. The signal is only emitted when clicked on a valid date, e.g., dates are not outside the minimum date and maximum date. If the selection mode is NoSele
2 min read
PyQt5 QCommandLinkButton - Clicked Signal
In this article we will see how we can get the clicked signal of the QCommandLinkButton. This signal is emitted when the button is activated, when the shortcut key is typed, or when click or animateClick method is called. Notably, this signal is not emitted if you call setDown, setChecked or toggle method. In order to do this we use clicked method
2 min read
Displaying the coordinates of the points clicked on the image using Python-OpenCV
OpenCV helps us to control and manage different types of mouse events and gives us the flexibility to operate them. There are many types of mouse events. These events can be displayed by running the following code segment : import cv2 [print(i) for i in dir(cv2) if 'EVENT' in i] Output : EVENT_FLAG_ALTKEY EVENT_FLAG_CTRLKEY EVENT_FLAG_LBUTTON EVENT
3 min read
PyQtGraph – Export Clicked Signal of Image View
In this article, we will see how we can trigger the export clicked signal of the image view object in PyQTGraph. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, vi
4 min read
PyQtGraph – Menu Clicked Signal of Image View
In this article, we will see how we can trigger the menu clicked signal of the image view object in PyQTGraph. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, vide
4 min read
PyQtGraph – ROI Clicked Signal of Image View
In this article we will see how we can trigger the roi clicked signal of image view object in PyQTGraph. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc
4 min read
Draw lines at the respective positions clicked by the mouse using Turtle
In this article, we will learn how to draw lines at any position which is clicked by the mouse using a turtle module. Turtle graphics:  turtle.listen(): Using this then we can give keyboard inputsturtle.onscreenclick(func,1 or 3): This function is used to bind function to a mouse-click event on canvas. 1 means left click and 3 means to right-click.
3 min read
Change color of button in Python - Tkinter
Prerequisite: Creating a button in tkinter, Python GUI – tkinter In this article, we are going to write a Python script to change the color of the button in Tkinter. It can be done with two methods: Using bg properties.Using activebackground properties. Example 1: using bg properties. We can change the button background color with bg properties, Th
1 min read
Python | Add style to tkinter button
Tkinter is a Python standard library that is used to create GUI (Graphical User Interface) applications. It is one of the most commonly used packages of Python. Tkinter supports both traditional and modern graphics support with the help of Tk themed widgets. All the widgets that Tkinter also has available in tkinter.ttk.Adding style in a tkinter.tt
4 min read
Practice Tags :