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
How to temporarily remove a Tkinter widget without using just .place?
In Tkinter applications, you may need to temporarily remove widgets from the display without destroying them. While place() geometry manager uses place_forget(), other geometry managers have their own forget methods: pack_forget() and grid_forget().
Using place_forget() with place()
The place() geometry manager positions widgets using absolute or relative coordinates. Use place_forget() to temporarily hide widgets placed with place() ?
from tkinter import *
# Create an instance of window
win = Tk()
win.geometry("700x300")
def forget_label():
label.place_forget()
def show_label():
label.place(relx=0.5, rely=0.2, anchor=CENTER)
# Create a label widget
label = Label(win, text="This is a new Label text", font='Arial 17 bold')
label.place(relx=0.5, rely=0.2, anchor=CENTER)
# Create buttons
button1 = Button(win, text="Remove It", command=forget_label)
button1.place(relx=0.4, rely=0.5, anchor=CENTER)
button2 = Button(win, text="Show It", command=show_label)
button2.place(relx=0.6, rely=0.5, anchor=CENTER)
win.mainloop()
Using pack_forget() with pack()
The pack() geometry manager organizes widgets in blocks. Use pack_forget() to temporarily remove packed widgets ?
from tkinter import *
win = Tk()
win.geometry("400x300")
def toggle_label():
if label.winfo_viewable():
label.pack_forget()
button.config(text="Show Label")
else:
label.pack(pady=20)
button.config(text="Hide Label")
# Create widgets
label = Label(win, text="This label uses pack()", font='Arial 14 bold', bg='lightblue')
label.pack(pady=20)
button = Button(win, text="Hide Label", command=toggle_label)
button.pack(pady=10)
win.mainloop()
Using grid_forget() with grid()
The grid() geometry manager arranges widgets in a table-like structure. Use grid_forget() for widgets positioned with grid() ?
from tkinter import *
win = Tk()
win.geometry("400x300")
def toggle_widgets():
if entry.winfo_viewable():
entry.grid_forget()
entry_label.grid_forget()
button.config(text="Show Entry")
else:
entry_label.grid(row=0, column=0, padx=10, pady=10)
entry.grid(row=0, column=1, padx=10, pady=10)
button.config(text="Hide Entry")
# Create widgets using grid
entry_label = Label(win, text="Name:", font='Arial 12')
entry_label.grid(row=0, column=0, padx=10, pady=10)
entry = Entry(win, font='Arial 12')
entry.grid(row=0, column=1, padx=10, pady=10)
button = Button(win, text="Hide Entry", command=toggle_widgets)
button.grid(row=1, column=0, columnspan=2, pady=20)
win.mainloop()
Comparison of Forget Methods
| Geometry Manager | Forget Method | Best For |
|---|---|---|
place() |
place_forget() |
Absolute positioning |
pack() |
pack_forget() |
Simple layouts |
grid() |
grid_forget() |
Complex table layouts |
Key Points
- Forget methods only hide widgets temporarily − they don't destroy them
- To show the widget again, call the original geometry method (
place(),pack(), orgrid()) - Use
winfo_viewable()to check if a widget is currently visible - Always match the forget method with the geometry manager used
Conclusion
Use the appropriate forget method based on your geometry manager: place_forget() for place(), pack_forget() for pack(), and grid_forget() for grid(). These methods provide a clean way to temporarily hide widgets without destroying them.
