58

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?

Laurel
  • 5,965
  • 14
  • 31
  • 57
Taylor Hill
  • 1,053
  • 1
  • 14
  • 24

3 Answers3

114

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • The method does not work for me. The error message: ``File "C:\Users\Wei-shan\Desktop\macauWorldHeritageSites.py", line 127, in 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'`` – Wei Shan Lee Jun 13 '21 at 09:35
  • @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. – Bryan Oakley Jun 13 '21 at 17:23
  • I 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)` Then `scene.delete()` – Wei Shan Lee Jun 14 '21 at 02:34
  • Well, that why my program getting slower every time I update the canvas. – M lab Dec 06 '22 at 10:36
12

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, and move to modify the items, or use delete to remove them.

Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
  • 1
    Is there a way to draw without persistence? – Taylor Hill Apr 05 '13 at 17:43
  • 3
    @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 do `canvas.delete('my_tag')`. All canvas widgets tagged with `'my_tag'` will be deleted. – Steven Rumbalski Apr 05 '13 at 17:50
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.

Community
  • 1
  • 1
DaveTheScientist
  • 3,299
  • 25
  • 19
  • If 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? – Taylor Hill Apr 05 '13 at 17:45
  • 1
    @TaylorHill Old thread, but I've still got to point this out: Canvas item ids are all unique, you're talking about a tag. – Artemis Apr 07 '19 at 20:14