
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Disable and Enable a Button in Tkinter
There are various attributes and properties in each tkinter widget to help us extend the functionality of the application. Tkinter Button widgets can be enabled and disabled by defining its state in the Button Object. The state attribute generally accepts two values Normal and Disabled which are used for enabling and disabling the button, respectively.
Example
#Import necessary Library from tkinter import * from tkinter import ttk from tkinter.filedialog import asksaveasfile #Create an instance of tkinter window win= Tk() #Set the geometry of tkinter window win.geometry("750x250") #Define the function to change the value in label widget def change_text(label): label.configure(text= "Hey, I am Label-2", background="gray91") #Create a Label label = Label(win, text= "Hey, I am Label-1", font= ('Helvetica 15 underline'), background="gray76") label.pack(pady=20) #Create a button btn= ttk.Button(win,text= "Change", command= lambda:change_text(label), state= DISABLED) btn.pack(pady=10) win.mainloop()
Output
Running the above code will display a window that has a disabled button. We can change the state of the button to Normal.
Now, change the value of the state attribute to NORMAL to make the button active on the window.
Advertisements