Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Python Tkinter – Changing the state of buttons

  • Isha BansalIsha Bansal
  • December 31, 2021
  • Tkinter
Python Tkinter – Changing the state of buttons

In this Python tutorial, we will learn how to use Tkinter to examine and alter the states of buttons.

To begin, we must first install Tkinter in our Python environment. We’ll see a graphical user interface window where we may modify the status of the Tkinter button by pressing it.

First, we must grasp what the state is in Python Tkinter.

Tkinter is a Python package that allows us to design our own graphical user interface (GUI). The Tkinter button has two states: normal and disabled. In the regular condition, we can push the button; but, in the disabled state, we cannot click the button.

So, in this tutorial, we’ll learn about Tkinter button states.


Implementing Button State

Let’s get started with the coding component of the provided challenge. First, import the Tkinter package. Now we’ll create an app object and set the window size to 200 x 200.

import tkinter as tk
app = tk.Tk()
app.geometry("200x200")

We’ll add two more buttons, button 1 and button 2. We’ll offer an argument as an app that will be displayed in the app window, and we’ll give it a name by setting text attributes to “Python Button 1.”

By providing the value tk to the state of button 1, we will indicate that it is disabled. DISABLED.

Similarly, in Button 2, we will supply the instruction as to some function name, so that when we hit that button, the following function will be executed.

As a function, we provide them switchButtonState. That function will be defined later. We’ll also use the pack to place those two buttons.

button1 = tk.Button(app, text="Python Button 1",state=tk.DISABLED)
button2 = tk.Button(app, text="EN/DISABLE Button 1",command = switchButtonState)
button1.pack()
button2.pack()

The function below will alter the status of the button. By clicking the button, if the status of button1 is normal, it will be changed to disabled; else, it will remain normal.

def switchButtonState():
    if (button1['state'] == tk.NORMAL):
        button1['state'] = tk.DISABLED
    else:
        button1['state'] = tk.NORMAL

Finally, we must use app.mainloop() to make the app execute.

app.mainloop()

By clicking on Button2, you may now modify the status of that Button1.


Changing the state of buttons using Python tkinter

import tkinter as tk

def switchButtonState():
    if (button1['state'] == tk.NORMAL):
        button1['state'] = tk.DISABLED
    else:
        button1['state'] = tk.NORMAL

app = tk.Tk()
app.geometry("200x200")
button1 = tk.Button(app, text="Python Button 1",state=tk.DISABLED)
button2 = tk.Button(app, text="EN/DISABLE Button 1",command = switchButtonState)
button1.pack()
button2.pack()

app.mainloop()

Output Screens

Change Btn States Output 1
Change Btn States Output 1
Change Btn States Output 2
Change Btn States Output 2

Conclusion

Congratulations! You just learned how to Changing the state of buttons. Hope you enjoyed it! 😇

Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:

  1. Python Tkinter Tutorial: Understanding the Tkinter Font Class
  2. Python Tkinter Project: Random mobile number generator
  3. Tkinter StringVar with Examples – Tkinter Tutorial
  4. Tkinter IntVar – Tkinter Tutorial with Examples

Thank you for taking your time out! Hope you learned something new!! 😄


Isha Bansal
Isha Bansal

Hey there stranger!
Do check out my blogs if you are a keen learner!

Hope you like them!

Articles: 185
4 Easy Plotting Libraries for Python With Examples
Previous Post 4 Easy Plotting Libraries for Python With Examples
Next Post How to Get Phone Number Information in Python?
How to Get Phone Number Information in Python?

Related Posts

Tkinter IntVar – Tkinter Tutorial with Examples

Tkinter IntVar – Tkinter Tutorial with Examples

  • July 5, 2026
Tkinter StringVar with Examples – Tkinter Tutorial

Tkinter StringVar with Examples – Tkinter Tutorial

  • July 5, 2026
Tkinter GUI Widgets

Tkinter GUI Widgets

  • April 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.