The Wayback Machine - https://web.archive.org/web/20240929151134/https://www.geeksforgeeks.org/how-to-change-tkinter-button-state/
Open In App

How to Change Tkinter Button State?

Last Updated : 17 Dec, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Tkinter is a Python Package for creating GUI applications. Python has a lot of GUI frameworks, but Tkinter is the only framework that’s built into the Python standard library. Tkinter has several strengths; it’s cross-platform, so the same code works on Windows, macOS, and Linux. Tkinter is lightweight and relatively painless to use compared to other frameworks.

In this article, we are going to learn how we can change the state of a Button.

Let’s understand this with step-wise:

Step 1: First we are going to import the Tkinter module and some widgets that we need.

Python3




# importing tkinter module
# along with some constants and Widgets
  
from tkinter import Tk
from tkinter.constants import DISABLED, NORMAL
from tkinter.ttk import Button, Label


If you are using Python2 then change tkinter to Tkinter and also tkinter.ttk will not also work so import widgets from Tkinter itself.

Step 2: Now we are going to create an App class that will contain all the Buttons and Labels.

Python3




# Creating App class which
# will contain our overall App
class App:
    def __init__(self, master) -> None:
  
        # Instantiating master i.e toplevel Widget
        self.master = master
  
        # Creating label
        self.label = Label(self.master,
                           text = "Click Button2 to change Button1 State")
        self.label.pack(pady = 10)
  
        # Creating button1
        # We will change the state of this Button
        # it has a initial state of "NORMAL" 
        # i.e Button can be pressed
        self.button1 = Button(self.master,
                              text = "Button1",
                              state = NORMAL)
          
        self.button1.pack(pady = 20)
  
        # Creating another button
        # We will use this button to
        # change the State of first button
        self.button2 = Button(self.master,
                              text = "Button2",
                              command = self.changeState)
          
        self.button2.pack(pady = 20)


Step 3: As you can see in the above code, we have a function attached with Button2 i.e changeState function next we are going to implement this function. In this function, we will change the State of Button1.

Python3




# Helper function which will change the State of Button1
def changeState(self) -> None:
  
    # Printing the State of 
    # the Button before ALTERING it
    # This is optional
    print(self.button1['state'])
  
    # Checking if the STATE of the Button1
  
    # If the STATE is NORMAL
    if (self.button1['state'] == NORMAL):
  
        # Change the state to DISABLED
        self.button1['state'] = DISABLED
    else:
        
        # Otherwise, change the state to NORMAL
        self.button1['state'] = NORMAL


Step 4: In this step, we will create the main function that will run this application. In the main function, we will set the application title and geometry and instantiate our App class.

Python3




if __name__ == "__main__":
  
    # Instantiating top level
    root = Tk()
  
    # Setting the title of the window
    root.title("Button State App")
  
    # Setting the geometry i.e Dimensions
    root.geometry("400x250")
  
    # Calling our App
    app = App(root)
  
    # Mainloop which will cause
    # this toplevel to run infinitely
    root.mainloop()


Below is the full implementation:

Python3




# importing tkinter module
# along with some constants and Widgets
from tkinter import Tk
from tkinter.constants import DISABLED, NORMAL
from tkinter.ttk import Button, Label
  
# Creating App class
# which will contain our overall App
class App:
    def __init__(self, master) -> None:
  
        # Instantiating master 
        # i.e toplevel Widget
        self.master = master
  
        # Creating label
        self.label = Label(self.master,
                           text="Click Button2 to change Button1 State")
          
        self.label.pack(pady = 10)
  
        # Creating button1
        # We will change the state of this Button
        # it has a initial state of 
        # "NORMAL" i.e Button can be pressed
        self.button1 = Button(self.master, 
                              text = "Button1", 
                              state = NORMAL)
          
        self.button1.pack(pady = 20)
  
        # Creating another button
        # We will use this button
        # to change the State of first button
        self.button2 = Button(self.master,
                              text = "Button2",
                              command = self.changeState)
          
        self.button2.pack(pady = 20)
  
    # Helper function which will 
    # change the State of Button1
    def changeState(self) -> None:
  
        # Printing the State of 
        # the Button before ALTERING it
        # This is optional
        print(self.button1['state'])
  
        # Checking if the STATE of the Button1
        # If the STATE is NORMAL
        if (self.button1['state'] == NORMAL):
  
            # Change the state to DISABLED
            self.button1['state'] = DISABLED
        else:
  
            # Otherwise, change the state to NORMAL
            self.button1['state'] = NORMAL
  
  
if __name__ == "__main__":
  
    # Instantiating top level
    root = Tk()
  
    # Setting the title of the window
    root.title("Button State App")
  
    # Setting the geometry i.e Dimensions
    root.geometry("400x250")
  
    # Calling our App
    app = App(root)
  
    # Mainloop which will cause this toplevel
    # to run infinitely
    root.mainloop()


Output:



Previous Article
Next Article

Similar Reads

Change color of button in Python - 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.Prerequisite: Creating a button in tkinter, Python GUI – Tkinter Change the color of the button in Python - TkinterExample 1: using bg properties. If you want to
2 min read
PyQt5 - Change state of Radio Button
In this article we will see how to change the state of radio button. Although with the help of setChecked method we can make the radio button checked but when we use this method on the checked radio button there will be no change in the state of radio button. In order to change the state of radio button when push button is pressed we have to do the
2 min read
PyQt5 - Change color of Radio button for unchecked state
In this article we will see how to change the color of radio button when it is in unchecked state, by default there is no color set to any state of radio. In order to set color to radio button for unchecked state we have to change the style sheet for the checked state of radio button. Below is the style sheet code. QRadioButton::unchecked { backgro
2 min read
PyQt5 – Change color of Radio button for checked state
In this article we will see how to change the color of radio button when it is in checked state, by default there is no color set to any state of radio button. In order to set color to radio button for checked state we have to change the style sheet for the checked state of radio button. Below is the style sheet code. QRadioButton::checked { backgr
2 min read
PyQt5 – Change color of pressed Radio button for unchecked state
In this article we will see how we can change the color of pressed radio button when it is in unchecked state, by default there is no color set to any state or when radio button get pressed. In order to set color to pressed radio button for unchecked state we have to change the style sheet for the unchecked state and when radio button get pressed,
2 min read
PyQt5 – Change color of Radio button for unchecked state when hover
In this article we will see how we can change the color of radio button when it is in unchecked state and mouse hover over it, by default there is no color set to any state or when mouse hover over it of radio button. In order to set color to radio button for unchecked state and mouse hover over it we have to change the style sheet for the unchecke
2 min read
PyQt5 – Change color of Radio button for checked state when hover
In this article we will see how we can change the color of radio button when it is in checked state and mouse hover over it, by default there is no color set to any state or when mouse hover over it of radio button. In order to set color to radio button for checked state and mouse hover over it we have to change the style sheet for the checked stat
2 min read
PyQt5 – Change color of pressed Radio button for checked state
In this article we will see how we can change the color of pressed radio button when it is in checked state, by default there is no color set to any state or when radio button get pressed. In order to set color to pressed radio button for checked state we have to change the style sheet for the checked state and when radio button get pressed, below
2 min read
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
Build GUI Application for Guess Indian State using Tkinter Python
In this article, you will learn to make a GUI game in Python, in which we will be shown a map of Indian states and an input prompt were in the prompt, the user has to enter a state name of India. If the user guessed it correctly, the name would hover over its location on the map. You can also see the score as the title of the input prompt window. M
8 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
Python | Add image on a Tkinter button
Tkinter is a Python module which is used to create GUI (Graphical User Interface) applications with the help of varieties of widgets and functions. Like any other GUI module it also supports images i.e you can use images in the application to make it more attractive. Image can be added with the help of PhotoImage() method. This is a Tkinter method
3 min read
Open a new Window with a button in Python-Tkinter
Python provides a variety of GUI (Graphic User Interface) such as PyQt, Tkinter, Kivy and soon. Among them, tkinter is the most commonly used GUI module in Python since it is simple and easy to learn and implement as well. The word Tkinter comes from the tk interface. The tkinter module is available in Python standard library.Note: For more informa
3 min read
Tkinter - Button that changes its properties on hover
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using tkinter is an easy task.
2 min read
How To Dynamically Resize Button Text in Tkinter?
Prerequisite: Python GUI – tkinter, Dynamically Resize Buttons When Resizing a Window using Tkinter In this article, we will see how to make the button text size dynamic. Dynamic means whenever button size will change, the button text size will also change. In Tkinter there is no in-built function, that will change the button text size dynamically.
3 min read
How to Pass Arguments to Tkinter Button Command?
When a user hits the button on the Tkinter Button widget, the command option is activated. In some situations, it's necessary to supply parameters to the connected command function. In this case, the procedures for both approaches are identical; the only thing that has to vary is the order in which you use them. Method 1: Pass Arguments to Tkinter
2 min read
How to Set Text of Tkinter Text Widget With a Button?
Prerequisite: Python GUI – tkinter Text Widget is used where a user wants to insert multi-line text fields. In this article, we are going to learn the approaches to set the text inside the text fields of the text widget with the help of a button. Approach: Using insert and delete methodImport the Tkinter module.Create a GUI window.Create our text w
2 min read
How to Close a Tkinter Window With a Button?
Prerequisites: Tkinter Python's Tkinter module offers the Button function to create a button in a Tkinter Window to execute any task once the button is clicked. The task can be assigned in the command parameter of Button() function. Given below are various methods by which this can be achieved. Method 1: Using destroy() Non-Class method Approach: I
3 min read
How to Bind Multiple Commands to Tkinter Button?
The button widget in Tkinter provides a way to interact with the application. The user presses a button to perform certain actions that are attached to that button. In general, we user provides a single action to a button but what if the user wants to attach more than one action to a button. In this article, we are going to see how we can bind more
4 min read
How to create a pop up message when a button is pressed in Python - Tkinter?
In this article, we will see how to create a button and how to show a popup message when a button is pressed in Python. Tkinter is a standard Python Package for creating GUI applications. Tkinter may be a set of wrappers that implement the Tk widgets as Python classes. Python, when combined with Tkinter, provides a quick and straightforward way to
2 min read
How do you create a Button on a tkinter Canvas?
In this article, we will see how to create a button on a Tkinter Canvas. The Canvas widget display various graphics on the application. It can be used to draw simple shapes to complicated graphs. We can also display various kinds of custom widgets according to our needs. It is used trigger any function which is presented in the code Intro to code :
1 min read
How to Use Bitmap images in Button in Tkinter?
Prerequisite: Python GUI – tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. A bitmap is an array of binary data representing the values of pixels in an image. A GIF i
2 min read
On/Off Toggle Button Switch in Tkinter
Prerequisite: Tkinter Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. In this article, we wi
2 min read
How to place a button at any position in Tkinter?
Prerequisite: Creating a button in Tkinter Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in Python. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most com
2 min read
How to move a Tkinter button?
Prerequisite: Creating a button in tkinter Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in Python. It is a standard Python interface to the Tk GUI toolkit shipped with Python. As Tk and Tkinter are available on most of the Unix platforms as well as on the Windows system, developing GUI applications with Tk
4 min read
How to add a border color to a button in Tkinter?
In this article, we will learn how to add border color to a button in Tkinter. In the first example, we are using the frame widget to add border color to a button in frame by highlighting the border with black color and a thickness of 2. Example 1: Using Frame widget to add border color to a button. Steps: A frame is a container for widgets. In thi
2 min read
Create an Import File Button with Tkinter
Graphical User Interfaces (GUIs) play a crucial role in enhancing the user experience of applications. Tkinter, a built-in Python library, provides a simple way to create GUI applications. In this article, we will focus on creating an "Import File" button using Tkinter. Create An Import File Button With TkinterIn this section, we will create an imp
3 min read
How to Clear the Entry Widget After Button Press in Tkinter
The entry widget in Tkinter is a common input field that allows users to enter and edit a single line of text. Clearing the contents of this widget programmatically can enhance user interaction, especially after submitting or resetting a form. In this article, we will explore two different approaches to clear the entry after a button is pressed in
2 min read
How To Disable / Enable A Button In Tkinter?
Disabling and enabling buttons in Tkinter is a fundamental aspect of creating interactive and user-friendly GUI applications. While the basic method involves changing the button's state property, there are various ways to achieve this based on different use cases and requirements. In this article, we'll explore different approaches to enable and di
2 min read
How to check which Button was clicked in Tkinter ?
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 in Tkinter. Steps to Check which Button was clicked in TkinterStep
2 min read