How to make a Tkinter widget invisible?


To make a tkinter widget invisible, we can use the pack_forget() method. It is generally used to unmap the widgets from the window.

Example

In the following example, we will create a label text and a button that can be used to trigger the invisible event on the label text widget.

#Import the required libraries
from tkinter import *

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

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

#Set the resizable property False
win.resizable(False, False)

#Make the widgets Invisible
def make_invisible(widget):
   widget.pack_forget()

#Create a label for the window or frame
label=Label(win, text="Hello World!", font=('Helvetica bold',20),
anchor="center")
label.pack(pady=20)

#Create a button to make the widgets invisible
btn=Button(win, text="Click", font= ('Helvetica bold', 10), command=lambda:
make_invisible(label))
btn.pack(pady=20)

win.mainloop()

Output

Running the above code will produce the following window −

Now click the “Click” Button to make the text label invisible.

Updated on: 26-Mar-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements