How to clear Tkinter Canvas?
Solution 1
Every canvas item is an object that Tkinter keeps track of. If you are clearing the screen by just drawing a black rectangle, then you effectively have created a memory leak -- eventually your program will crash due to the millions of items that have been drawn.
To clear a canvas, use the delete method. 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):
canvas.delete("all")
If you want to delete only certain items on the canvas (such as foreground objects, while leaving the background objects on the display) you can assign tags to each item. Then, instead of "all"
, you could supply the name of a tag.
If you're creating a game, you probably don't need to delete and recreate items. For example, if you have an object that is moving across the screen, you can use the move or coords method to move the item.
Solution 2
Items drawn to the canvas are persistent. create_rectangle
returns an item id that you need to keep track of. If you don't remove old items your program will eventually slow down.
From Fredrik Lundh's An Introduction to Tkinter:
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
coords
,itemconfig
, andmove
to modify the items, or usedelete
to remove them.
Solution 3
Yes, I believe you are creating thousands of objects. If you're looking for an easy way to delete a bunch of them at once, use canvas tags described here. This lets you perform the same operation (such as deletion) on a large number of objects.
Taylor Hill
Updated on July 09, 2022Comments
-
Taylor Hill almost 2 years
When I draw a shape using:
canvas.create_rectangle(10, 10, 50, 50, color="green")
Does Tkinter keep track of the fact that it was created?
In a simple game I'm making, 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, and so on.Am I creating thousands of rectangle objects in memory?
I know you can assign the code above to a variable, but if I don't do that and just draw directly to the canvas, does it stay in memory, or does it just draw the pixels, like in the HTML5 canvas?
-
Taylor Hill about 11 yearsIs there a way to draw without persistence?
-
Taylor Hill about 11 yearsIf I assign a bunch of shapes the same id, such as "tile", and I do the delete method on the id "tile", does it delete all of them at once?
-
Steven Rumbalski about 11 years@TaylorHill: No. But as DaveTheScientist you can use canvas tags to collect your canvas widgets. Create the widget adding the argument
tags='my_tag'
. Then when you want to clear the screen you can docanvas.delete('my_tag')
. All canvas widgets tagged with'my_tag'
will be deleted. -
Artemis about 5 years@TaylorHill Old thread, but I've still got to point this out: Canvas item ids are all unique, you're talking about a tag.
-
Andrei over 4 yearsHow do you get the name of the tag, if you are not assigning the line to a variable?
-
Wei Shan Lee almost 3 yearsThe method does not work for me. The error message:
File "C:\Users\Wei-shan\Desktop\macauWorldHeritageSites.py", line 127, in <module> canvas.delete("all") File "D:\anaconda3\lib\site-packages\vpython\vpython.py", line 2836, in delete self.addmethod('delete','None') AttributeError: 'str' object has no attribute 'addmethod'
-
Russell Smith almost 3 years@LEEWEISHAN: that error is telling you that your
canvas
variable contains a string. It's not a canvas so it doesn't have the same methods as a canvas. -
Wei Shan Lee almost 3 yearsI modified it a little bit by setting up an instance for the canvas first:
scene = canvas(center = vector(0.5,0,5,0), background = color.white)
Thenscene.delete()