How to clear Tkinter Canvas using Python?
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?
To clear a Tkinter clear 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 :
canvas.delete("all")
Note : Here the string "all" is a special tag that represents all items on the canvas.