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.
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()
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:
- Python Tkinter Tutorial: Understanding the Tkinter Font Class
- Python Tkinter Project: Random mobile number generator
- Tkinter StringVar with Examples – Tkinter Tutorial
- Tkinter IntVar – Tkinter Tutorial with Examples
Thank you for taking your time out! Hope you learned something new!! 😄