How to I clear Tkinter Canvas using Python

0 votes

When I draw a shape using:

canvas.create_rectangle(15, 15, 60, 60, color="blue")

Does Tkinter keep track of the fact that it was created?

I am making a game where my code has one Frame create a bunch of rectangles and then draw a big black rectangle to clear the screen and then draw another set of updated rectangles.

Am I creating thousands of rectangle objects in memory? What is the right way to do it? Appreciate some help here!

Dec 24, 2018 in Python by Anirudh
• 2,080 points

1 answer to this question.

0 votes

To clear a canvas, use the delete method. 

This ensures you avoid memory leaks and not end up creating thousands of objects.

Give it the special parameter "all" to delete all items on the canvas (the string "all"" is a special tag that represents all items on the canvas):


Hope this helps!!

If you need to learn more about Python, It's recommended to join Python Programming course today.


answered Dec 24, 2018 by Nymeria
• 3,560 points

can I add widgets to it after using this code 


What I did is, I created a button, if it is clicked it creates a new screen(basically just deletes everything off the canvas), can I add widgets to that blank canvas?

Hi, @There,

Yes, you can add widgets,

To display things on the canvas, you create one or more canvas items, which are placed in a stack. By default, new items are drawn on top of items already on the canvas.

Tkinter provides lots of methods allowing you to manipulate the items in various ways. Among other things, you can attach (bind) event callbacks to individual canvas items.

To draw things in the canvas, use the create methods to add new items.

from Tkinter import *

master = Tk()

w = Canvas(master, width=200, height=100)

w.create_line(0, 0, 200, 100)
w.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))

w.create_rectangle(50, 25, 150, 75, fill="blue")


Note that items added to the canvas are kept until you remove them. If you want to change the drawing, you can either use methods like coordsitemconfig, and move to modify the items or use delete to remove them.

i = w.create_line(xy, fill="red")

w.coords(i, new_xy) # change coordinates
w.itemconfig(i, fill="blue") # change color

w.delete(i) # remove

w.delete(ALL) # remove all items

Related Questions In Python

0 votes
1 answer

How can I expose callbacks to Fortran using Python

The code that I've written below. The ...READ MORE

answered Aug 24, 2018 in Python by aryya
• 7,450 points
0 votes
2 answers

How can I write a program to add two numbers using functions in python?

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
0 votes
0 answers

How can I convert a list to a string using Python?

How can I convert a list to ...READ MORE

Sep 21, 2022 in Python by Samuel
• 460 points
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,058 views
0 votes
1 answer
0 votes
1 answer

How to put a variable inside a String using Python?

In the easiest way, you can create ...READ MORE

answered Nov 23, 2018 in Python by Nymeria
• 3,560 points

edited Dec 12, 2018 by Nymeria 896 views
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP