HOWTO · Tkinter
How to Hide, Recover and Delete Tkinter Widgets
This tutorial demonstrates how to hide, recover and delete Tkinter widgets by clicking a button.
On this page
Use a geometry manager’s forget method to hide a Tkinter widget while keeping it available, and call its geometry manager again to show it. Use destroy() when the widget should be removed permanently; calling pack() or grid() cannot bring a destroyed widget back. The examples use button callbacks to control a label. See the Tkinter button guide for more on button callbacks.
Hide and Recover Tkinter Widgets
When a widget was added with pack(), call pack_forget() to remove it from the display. The widget still exists, so pack() can display it again. With grid-managed widgets, grid_forget() discards the saved grid settings, while grid_remove() keeps them for a later grid() call. Use one geometry manager for each widget; do not mix pack() and grid() among widgets that share a parent. The example below gives each label its own frame: pack-managed controls use pack(), and grid-managed controls use grid() only inside their frame.
This headless example prints the method choices for the three hide-and-recover cases. In a Tkinter window, bind the corresponding method calls to buttons; the actual GUI needs a desktop display.
methods = (
("hide a packed widget", "pack_forget()"),
("show a packed widget", "pack()"),
("hide and later restore grid placement", "grid_remove() then grid()"),
("hide and specify grid placement again", "grid_forget() then grid(row=0, column=0)"),
)
for task, method in methods:
print(f"{task}: {method}")
hide a packed widget: pack_forget()
show a packed widget: pack()
hide and later restore grid placement: grid_remove() then grid()
hide and specify grid placement again: grid_forget() then grid(row=0, column=0)
This Python 3 example creates real Tkinter buttons and connects each button to the label operation named on it. Run it in a desktop session with Tk support to use the window.
import tkinter as tk
try:
root = tk.Tk()
except tk.TclError:
print("This example needs a desktop display with Tk support.")
raise SystemExit(0)
root.title("Hide and recover labels")
pack_frame = tk.Frame(root, padx=8, pady=8)
pack_frame.pack(fill="x")
pack_label = tk.Label(pack_frame, text="Packed label")
pack_label.pack()
tk.Button(pack_frame, text="Hide packed label", command=pack_label.pack_forget).pack()
tk.Button(pack_frame, text="Show packed label", command=pack_label.pack).pack()
grid_frame = tk.Frame(root, padx=8, pady=8)
grid_frame.pack(fill="x")
grid_label = tk.Label(grid_frame, text="Grid label")
grid_label.grid(row=0, column=0, padx=4)
tk.Button(grid_frame, text="Hide and retain grid options", command=grid_label.grid_remove).grid(row=0, column=1, padx=4)
tk.Button(grid_frame, text="Restore retained grid options", command=grid_label.grid).grid(row=1, column=0, padx=4)
tk.Button(
grid_frame,
text="Forget grid options",
command=grid_label.grid_forget,
).grid(row=1, column=1, padx=4)
tk.Button(
grid_frame,
text="Restore at row 0, column 0",
command=lambda: grid_label.grid(row=0, column=0, padx=4),
).grid(row=2, column=0, columnspan=2, pady=4)
root.mainloop()
The packed label’s buttons call pack_forget() and pack() on that same label. The grid buttons demonstrate both recovery choices: grid_remove() retains the original row and column for the no-argument grid() callback, while grid_forget() clears those options, so its button supplies the intended placement explicitly. These calls change how each label is managed without destroying it. For more on widget placement, see Tkinter geometry managers.
Delete Tkinter Widgets Permanently by Clicking a Button
Call destroy() on the widget that should be removed. In this example, the Delete label button calls label.destroy, which removes the label, not the button. A destroyed widget cannot be restored with a geometry manager; create a new label if one is needed later. Run this Python 3 example in a desktop session with Tk support, then click Delete label to remove the label from the window.
import tkinter as tk
try:
root = tk.Tk()
except tk.TclError:
print("This example needs a desktop display with Tk support.")
raise SystemExit(0)
root.title("Destroy a label")
label = tk.Label(root, text="This label will be removed")
label.pack(padx=8, pady=8)
tk.Button(root, text="Delete label", command=label.destroy).pack(padx=8, pady=8)
root.mainloop()