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 hide and show canvas items on Tkinter?
The Canvas widget is one of the versatile widgets in Tkinter. It is used in many applications for designing the graphical user interface such as designing, adding images, creating graphics, etc. We can add widgets in the Canvas widget itself. The widgets that lie inside the canvas are sometimes called Canvas Items.
If we want to show or hide the canvas items through a Button, then this can be achieved by using the state property in itemconfig(id, state) method.
Syntax
The basic syntax for hiding and showing canvas items is ?
canvas.itemconfig(item_id, state='hidden') # Hide item canvas.itemconfig(item_id, state='normal') # Show item
Method 1: Using Rectangle and Text Items
Let's start with a simple example using basic canvas items like rectangles and text ?
import tkinter as tk
from tkinter import ttk
# Create main window
root = tk.Tk()
root.title("Hide/Show Canvas Items")
root.geometry("400x300")
# Global variable to track visibility
is_visible = True
def toggle_items():
global is_visible
if is_visible:
# Hide items
canvas.itemconfig(rect_id, state='hidden')
canvas.itemconfig(text_id, state='hidden')
button.config(text="Show Items")
is_visible = False
else:
# Show items
canvas.itemconfig(rect_id, state='normal')
canvas.itemconfig(text_id, state='normal')
button.config(text="Hide Items")
is_visible = True
# Create canvas
canvas = tk.Canvas(root, width=350, height=200, bg='white')
canvas.pack(pady=10)
# Add items to canvas and store their IDs
rect_id = canvas.create_rectangle(50, 50, 150, 100, fill='blue', outline='black')
text_id = canvas.create_text(100, 75, text="Canvas Item", fill='white', font=('Arial', 12))
# Create button to toggle visibility
button = ttk.Button(root, text="Hide Items", command=toggle_items)
button.pack(pady=10)
root.mainloop()
Method 2: Using Multiple Canvas Items
You can also hide/show multiple canvas items at once by storing their IDs in a list ?
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Hide/Show Multiple Items")
root.geometry("500x350")
# List to store canvas item IDs
item_ids = []
is_visible = True
def toggle_all_items():
global is_visible
state = 'hidden' if is_visible else 'normal'
button_text = "Show All" if is_visible else "Hide All"
# Toggle state for all items
for item_id in item_ids:
canvas.itemconfig(item_id, state=state)
button.config(text=button_text)
is_visible = not is_visible
# Create canvas
canvas = tk.Canvas(root, width=450, height=250, bg='lightgray')
canvas.pack(pady=10)
# Create multiple canvas items
item_ids.append(canvas.create_oval(50, 50, 100, 100, fill='red'))
item_ids.append(canvas.create_rectangle(150, 50, 200, 100, fill='green'))
item_ids.append(canvas.create_polygon(250, 50, 275, 100, 300, 50, fill='blue'))
item_ids.append(canvas.create_text(350, 75, text="Hello!", font=('Arial', 14)))
# Create line connecting shapes
item_ids.append(canvas.create_line(100, 75, 150, 75, width=3))
item_ids.append(canvas.create_line(200, 75, 250, 75, width=3))
# Button to toggle all items
button = ttk.Button(root, text="Hide All", command=toggle_all_items)
button.pack(pady=10)
root.mainloop()
Method 3: Selective Show/Hide
For more control, you can create separate functions to hide/show specific items ?
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Selective Hide/Show")
root.geometry("400x400")
def toggle_shapes():
current_state = canvas.itemcget(circle_id, 'state')
new_state = 'hidden' if current_state == 'normal' else 'normal'
canvas.itemconfig(circle_id, state=new_state)
canvas.itemconfig(square_id, state=new_state)
def toggle_text():
current_state = canvas.itemcget(text_id, 'state')
new_state = 'hidden' if current_state == 'normal' else 'normal'
canvas.itemconfig(text_id, state=new_state)
# Create canvas
canvas = tk.Canvas(root, width=350, height=250, bg='white', relief='sunken', bd=2)
canvas.pack(pady=10)
# Create canvas items
circle_id = canvas.create_oval(50, 50, 120, 120, fill='orange', outline='black', width=2)
square_id = canvas.create_rectangle(200, 50, 270, 120, fill='purple', outline='black', width=2)
text_id = canvas.create_text(175, 150, text="Canvas Text Item", font=('Arial', 12, 'bold'))
# Control buttons
frame = tk.Frame(root)
frame.pack(pady=10)
ttk.Button(frame, text="Toggle Shapes", command=toggle_shapes).pack(side='left', padx=5)
ttk.Button(frame, text="Toggle Text", command=toggle_text).pack(side='left', padx=5)
root.mainloop()
Key Points
- Use
itemconfig(item_id, state='hidden')to hide canvas items - Use
itemconfig(item_id, state='normal')to show canvas items - Store canvas item IDs returned by
create_*methods - You can check current state using
itemcget(item_id, 'state') - Hidden items are not destroyed, just made invisible
Conclusion
Hiding and showing canvas items in Tkinter is straightforward using the itemconfig() method with the state parameter. This technique is useful for creating interactive interfaces, animations, and dynamic content display.
