How to Change Tkinter Button State?
Last Updated :
17 Dec, 2020
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
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
class App:
def __init__( self , master) - > None :
self .master = master
self .label = Label( self .master,
text = "Click Button2 to change Button1 State" )
self .label.pack(pady = 10 )
self .button1 = Button( self .master,
text = "Button1" ,
state = NORMAL)
self .button1.pack(pady = 20 )
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
def changeState( self ) - > None :
print ( self .button1[ 'state' ])
if ( self .button1[ 'state' ] = = NORMAL):
self .button1[ 'state' ] = DISABLED
else :
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__" :
root = Tk()
root.title( "Button State App" )
root.geometry( "400x250" )
app = App(root)
root.mainloop()
|
Below is the full implementation:
Python3
from tkinter import Tk
from tkinter.constants import DISABLED, NORMAL
from tkinter.ttk import Button, Label
class App:
def __init__( self , master) - > None :
self .master = master
self .label = Label( self .master,
text = "Click Button2 to change Button1 State" )
self .label.pack(pady = 10 )
self .button1 = Button( self .master,
text = "Button1" ,
state = NORMAL)
self .button1.pack(pady = 20 )
self .button2 = Button( self .master,
text = "Button2" ,
command = self .changeState)
self .button2.pack(pady = 20 )
def changeState( self ) - > None :
print ( self .button1[ 'state' ])
if ( self .button1[ 'state' ] = = NORMAL):
self .button1[ 'state' ] = DISABLED
else :
self .button1[ 'state' ] = NORMAL
if __name__ = = "__main__" :
root = Tk()
root.title( "Button State App" )
root.geometry( "400x250" )
app = App(root)
root.mainloop()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...