How to gray out (disable) a Tkinter Frame?


A Tkinter frame widget can contain a group of widgets. We can change the state of widgets in the frame by enabling or disabling the state of its underlying frame. To disable all the widgets inside that particular frame, we have to select all the children widgets that are lying inside that frame using winfor_children() and change the state using state=(‘disabled’ or ‘enable’) attribute.

Example

In this example, we will create a button and an entry widget. Initially, the state of entry widget is disabled. But when we click the button, it will enable all the widgets in the frame.

#Import the required Libraries
from tkinter import *
from tkinter import ttk

#Define a Function to enable the frame
def enable(children):
   for child in children:
      child.configure(state='enable')

#Create an instance of tkinter frame
win = Tk()

#Set the geometry of tkinter frame
win.geometry("750x250")

#Creates top frame
frame1 = LabelFrame(win, width= 400, height= 180, bd=5)
frame1.pack()

#Create an Entry widget in Frame2
entry1 = ttk.Entry(frame1, width= 40)
entry1.insert(INSERT,"Enter Your Name")
entry1.pack()
entry2= ttk.Entry(frame1, width= 40)
entry2.insert(INSERT, "Enter Your Email")
entry2.pack()

#Creates bottom frame
frame2 = LabelFrame(win, width= 150, height=100)
frame2.pack()

#Create a Button to enable frame
button1 = ttk.Button(frame2, text="Enable", command=lambda: enable(frame1.winfo_children()))
button1.pack()
for child in frame1.winfo_children():
   child.configure(state='disable')

win.mainloop()

Output

Running the above code will display a window that contains two Label Frames. Each frame contains an entry widget and a button to enable or disable the frame.

When we click the "Enable" button, it will activate Frame1.

Updated on: 04-May-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements