Back

Explore Courses Blog Tutorials Interview Questions
+9 votes
4 views
in Python by (47.6k points)
recategorized by

When I draw a shape using the following piece of code:

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 which has one Frame create a bunch of rectangles, and then I 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?

1 Answer

+9 votes
by (106k points)
edited by

To clear a Tkinter-canvas, you can use the delete method. 

The delete method ensures you to avoid memory leaks and not end up creating thousands of objects.

As a parameter pass "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")

To know more about this you can have a look at the following video:-

Related questions

Browse Categories

...