Your Search Bar For Shrewd Tips

How Do I Clear Tkinter Canvas?


To clear a Tkinter Canvas, call its delete() method with the special tag "all":

canvas.delete("all")

This removes every item drawn on the canvas, including lines, shapes, text, and image items. Tkinter’s Canvas does not have a separate clear() method, so delete("all") is the usual way to empty it before drawing a new scene. The Canvas widget itself remains in place, along with its size, background color, bindings, and other settings.

Clear a Tkinter Canvas with delete("all")

Here is a complete example with a button that clears the canvas when clicked:

import tkinter as tk

root = tk.Tk()
root.title("Clear a Tkinter Canvas")

canvas = tk.Canvas(root, width=400, height=250, bg="white")
canvas.pack(padx=10, pady=10)

canvas.create_rectangle(40, 40, 180, 130, fill="skyblue")
canvas.create_text(250, 85, text="Canvas content")

def clear_canvas():
    canvas.delete("all")

clear_button = tk.Button(root, text="Clear", command=clear_canvas)
clear_button.pack(pady=(0, 10))

root.mainloop()

When the user clicks the button, Tkinter runs clear_canvas(). The call to canvas.delete("all") removes every canvas item, leaving a blank white drawing area. You can then add new items with methods such as create_line(), create_rectangle(), create_text(), or create_image().

What does delete("all") remove?

A Canvas is a widget that holds individual canvas items. Each shape, line, text label, image, or embedded window is an item that can be created, selected, moved, or deleted. The string "all" is a built-in tag that refers to every item on the Canvas. Therefore, canvas.delete("all") removes all of those items in one call.

Clearing the items does not destroy the Canvas widget. It also does not reset its configuration. For example, a custom background color, width, height, scroll settings, event bindings, and widget-level options remain unchanged. If you want to change the background too, configure it separately:

canvas.delete("all")
canvas.configure(bg="lightgray")

Likewise, clearing the Canvas does not automatically reset Python variables, counters, or other application data. If your program tracks a drawing state or a list of objects, update that state as part of your clear function.

Clear only selected Canvas items

You do not have to delete every item. The delete() method can also target an item by its numeric ID or by a tag. Each call to a create_... method returns an item ID, which you can save and use later:

rectangle_id = canvas.create_rectangle(
    30, 30, 140, 100, fill="orange"
)

canvas.delete(rectangle_id)

To remove a group of items, give them a shared tag when you create them. For example, tag all the objects associated with a moving character as "player":

canvas.create_oval(
    50, 50, 90, 90,
    fill="green",
    tags=("player",)
)
canvas.create_rectangle(
    60, 85, 80, 130,
    fill="blue",
    tags=("player",)
)

canvas.delete("player")

Deleting the "player" tag removes the oval and rectangle, while leaving unrelated items alone. Tags are useful when your program has layers or categories, such as a background, game pieces, selection outlines, or temporary guides. Choose specific tag names for your own groups. Avoid using "all" as a custom tag, because it has special meaning in Canvas operations.

Clear and redraw the Canvas

Many applications need to refresh the Canvas after data changes. A straightforward approach is to clear the old items and then draw the current scene again. Put the drawing code in a function so it can be reused:

def draw_scene():
    canvas.delete("all")

    canvas.create_rectangle(
        20, 20, 380, 220,
        outline="gray"
    )
    canvas.create_oval(
        140, 70, 260, 190,
        fill="coral"
    )
    canvas.create_text(
        200, 35,
        text="Current scene"
    )

draw_scene()

Call draw_scene() again whenever you want to rebuild the display. For example, a button can update the data and redraw the Canvas, or a game loop can refresh the scene after an object moves. Clearing first prevents each redraw from piling another copy of the same shapes on top of the previous ones.

For a small drawing, deleting everything and recreating the items is simple and reliable. For a large or frequently updated scene, it may be more efficient to keep item IDs and update existing items with methods such as coords() or itemconfigure(). For example, you can move a stored item rather than delete and recreate it:

circle_id = canvas.create_oval(
    20, 20, 60, 60, fill="purple"
)

canvas.coords(circle_id, 100, 100, 140, 140)

Clear a Canvas in a class

If your interface uses a class, make the Canvas a stored attribute and define a method to clear it. This keeps the clear operation easy to call from buttons and other methods:

import tkinter as tk

class DrawingApp:
    def __init__(self, root):
        self.canvas = tk.Canvas(
            root, width=400, height=250, bg="white"
        )
        self.canvas.pack()

        self.canvas.create_line(
            20, 20, 180, 120, width=3, fill="navy"
        )

        clear_button = tk.Button(
            root,
            text="Clear canvas",
            command=self.clear_canvas
        )
        clear_button.pack(pady=8)

    def clear_canvas(self):
        self.canvas.delete("all")

root = tk.Tk()
app = DrawingApp(root)
root.mainloop()

Notice that the button command is self.clear_canvas, without parentheses. This passes the method to Tkinter so it can call it when the button is pressed. Writing self.clear_canvas() in the button definition would run the method immediately while the interface is being created, rather than waiting for a click.

Clearing images and embedded widgets

Canvas image items are removed by delete("all"), just like shapes and text. However, Python image objects such as PhotoImage may need to remain referenced in your program for as long as they are displayed. If you redraw an image later, keep a reference to the new image object, for example on self or in a variable that stays in scope:

self.photo = tk.PhotoImage(file="picture.png")
self.image_id = self.canvas.create_image(
    100, 100,
    image=self.photo
)

Deleting the image item removes it from the Canvas, but it does not automatically clear your Python reference to self.photo. That is generally fine; you can keep or replace the reference according to your application’s needs.

Canvas items created with create_window() are a special case. Deleting the window item removes it from the Canvas display, but the embedded Tkinter widget itself may still exist as a child widget. If you are discarding that widget entirely, destroy it as well:

canvas.delete("all")
embedded_widget.destroy()

Keep a reference to the embedded widget if you need to destroy it later. If you only want to hide it temporarily, you may prefer to manage its display rather than destroy it.

Common mistakes when clearing a Tkinter Canvas

  • Calling a method that does not exist: Use canvas.delete("all"); a Tkinter Canvas has no built-in canvas.clear() method.
  • Deleting the widget instead of its contents: Calling canvas.destroy() destroys the Canvas widget itself. Use delete("all") when you want to keep the widget and remove only its items.
  • Using the wrong target: A tag or item ID deletes only matching items. Use the exact tag or ID you assigned, or use "all" to remove everything.
  • Forgetting to redraw: After clearing, the Canvas is blank until your code creates new items. Call your drawing function when you want the scene restored.
  • Accumulating items during updates: If you repeatedly draw the same content without deleting or updating existing items, the Canvas can become cluttered and may slow down. Clear first or update saved item IDs.

When should you use delete("all")?

Use canvas.delete("all") when you want a complete reset of the drawing area but plan to keep the Canvas widget. It is a good choice for a Clear button, starting a new drawing, changing screens in a simple game, or refreshing a small diagram. Use item IDs or tags when you want to remove only part of the display. If the Canvas widget itself should go away, destroy it and create a new one instead.

For most Tkinter programs, the basic pattern is simply:

def clear_canvas():
    canvas.delete("all")

Call that function from a button or from the part of your application that needs to reset the drawing. When you need a fresh display, clear the existing items and then redraw the content you want to show.

Conclusion

The standard way to clear a Tkinter Canvas is to call canvas.delete("all"). This removes every canvas item while preserving the Canvas widget and its configuration. To clear only selected content, delete an item by its ID or assign related items a shared tag. For refreshed scenes, place your drawing code in a reusable function and call it after clearing. Remember that embedded widgets may need to be destroyed separately, and that deleting Canvas items does not reset your application’s own data. With these patterns, you can clear, update, and redraw a Tkinter Canvas cleanly.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →