How to show and hide widgets in Tkinter?

Tkinter is a Python library used to create GUI-based applications. Sometimes you need to dynamically show or hide widgets based on user interactions or application state.

  • To display/show a widget, use pack() geometry manager
  • To hide any widget from the application, use pack_forget() method
  • You can also use grid() and grid_forget() for grid-based layouts

Basic Show/Hide Example

Here's how to create a toggle button that shows and hides a label widget ?

import tkinter as tk
from tkinter import ttk

# Create main window
window = tk.Tk()
window.geometry("400x200")
window.title("Show/Hide Widget Demo")

# Create a label to show/hide
label = tk.Label(window, text="Hello, World!", font=("Arial", 14), bg="lightblue")
label.pack(pady=20)

# Variable to track label state
is_visible = True

def toggle_label():
    global is_visible
    if is_visible:
        label.pack_forget()  # Hide the label
        button.config(text="Show Label")
        is_visible = False
    else:
        label.pack(pady=20)  # Show the label
        button.config(text="Hide Label")
        is_visible = True

# Create toggle button
button = tk.Button(window, text="Hide Label", command=toggle_label)
button.pack(pady=10)

window.mainloop()

Using Grid Layout

You can also show/hide widgets using grid geometry manager ?

import tkinter as tk

window = tk.Tk()
window.geometry("300x150")
window.title("Grid Show/Hide Demo")

# Create widgets
label1 = tk.Label(window, text="Widget 1", bg="red", fg="white")
label2 = tk.Label(window, text="Widget 2", bg="green", fg="white")
label3 = tk.Label(window, text="Widget 3", bg="blue", fg="white")

# Initially show all widgets
label1.grid(row=0, column=0, padx=5, pady=5)
label2.grid(row=0, column=1, padx=5, pady=5)
label3.grid(row=0, column=2, padx=5, pady=5)

def hide_middle():
    label2.grid_forget()

def show_middle():
    label2.grid(row=0, column=1, padx=5, pady=5)

# Control buttons
hide_btn = tk.Button(window, text="Hide Middle", command=hide_middle)
show_btn = tk.Button(window, text="Show Middle", command=show_middle)

hide_btn.grid(row=1, column=0, pady=10)
show_btn.grid(row=1, column=1, pady=10)

window.mainloop()

Multiple Widgets Control

Here's an example showing how to control multiple widgets at once ?

import tkinter as tk

window = tk.Tk()
window.geometry("350x250")
window.title("Multiple Widgets Control")

# Create multiple widgets
widgets = []
for i in range(3):
    widget = tk.Label(window, text=f"Label {i+1}", bg=["red", "green", "blue"][i], 
                     fg="white", width=15)
    widget.pack(pady=5)
    widgets.append(widget)

def hide_all():
    for widget in widgets:
        widget.pack_forget()
    hide_btn.config(state="disabled")
    show_btn.config(state="normal")

def show_all():
    for widget in widgets:
        widget.pack(pady=5)
    hide_btn.config(state="normal")
    show_btn.config(state="disabled")

# Control buttons
hide_btn = tk.Button(window, text="Hide All", command=hide_all)
show_btn = tk.Button(window, text="Show All", command=show_all, state="disabled")

hide_btn.pack(pady=10)
show_btn.pack()

window.mainloop()

Key Methods Comparison

Method Geometry Manager Purpose
pack() / pack_forget() Pack Show/hide packed widgets
grid() / grid_forget() Grid Show/hide grid widgets
place() / place_forget() Place Show/hide placed widgets

Conclusion

Use pack_forget(), grid_forget(), or place_forget() to hide widgets, and their corresponding show methods to display them again. This technique is useful for creating dynamic interfaces that respond to user actions.

Updated on: 2026-03-25T22:28:38+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements