Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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 widgets from the window temporarily without destroying them.
Methods to Make Widgets Invisible
There are three main approaches depending on your geometry manager:
-
pack_forget()- for widgets using pack() -
grid_forget()- for widgets using grid() -
place_forget()- for widgets using place()
Example Using pack_forget()
In this example, we create a label and a button that makes the label invisible when clicked ?
# 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()
Making Widget Visible Again
To make a forgotten widget visible again, you need to pack it again ?
from tkinter import *
win = Tk()
win.geometry("600x250")
# Toggle visibility function
def toggle_visibility():
if label.winfo_viewable():
label.pack_forget()
btn.config(text="Show")
else:
label.pack(pady=20)
btn.config(text="Hide")
label = Label(win, text="Toggle Me!", font=('Helvetica bold', 20))
label.pack(pady=20)
btn = Button(win, text="Hide", command=toggle_visibility)
btn.pack(pady=20)
win.mainloop()
Comparison of Methods
| Method | Use With | Widget State |
|---|---|---|
pack_forget() |
pack() geometry | Hidden, not destroyed |
grid_forget() |
grid() geometry | Hidden, not destroyed |
place_forget() |
place() geometry | Hidden, not destroyed |
Conclusion
Use pack_forget(), grid_forget(), or place_forget() to make Tkinter widgets invisible temporarily. The widget remains in memory and can be made visible again by calling the appropriate geometry method.
Advertisements
