Monday, November 18, 2024
Google search engine
HomeLanguagesHow to Change Tkinter Button State?

How to Change Tkinter Button State?

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:

RELATED ARTICLES

Most Popular

Recent Comments