python - How to clear Tkinter Canvas? -


when draw shape using:

canvas.create_rectangle(10, 10, 50, 50, color="green") 

does tkinter keep track of fact created?

in simple game i'm making, code has 1 frame create bunch of rectangles, , draw big black rectangle clear screen, , draw set of updated rectangles, , on.

am creating thousands of rectangle objects in memory?

i know can assign code above variable, if don't , draw directly canvas, stay in memory, or draw pixels, in html5 canvas?

every canvas item object tkinter keeps track of. if clearing screen drawing black rectangle, have created memory leak -- program crash due millions of items have been drawn.

to clear canvas, use delete method. give special parameter "all" delete items on canvas (the string "all"" special tag represents items on canvas):

canvas.delete("all") 

if want delete items on canvas (such foreground objects, while leaving background objects on display) can assign tags each item. then, instead of "all", supply name of tag.

if you're creating game, don't need delete , recreate items. example, if have object moving across screen, can use move or coords method move item.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -