How to clear Tkinter Canvas?

How to clear Tkinter Canvas ?

Asked on November 27, 2018 in tkinter.
Add Comment


  • 6 Answer(s)

    You can try this solution:

    The 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 http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.delete-method 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 http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.move-method or http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.coords-method method to move the item.

    Answered on November 27, 2018.
    Add Comment

    Here is the answer:

        The 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 http://effbot.org/tkinterbook/canvas.htm#patterns

    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.

    Answered on November 27, 2018.
    Add Comment

    The answer:

        I believe that  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 http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/canvas-tags.html. This lets you perform the same operation such as deletion on a large number of objects.

    Answered on November 27, 2018.
    Add Comment

    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")
    Answered on February 25, 2019.
    Add Comment

    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")
    Answered on April 17, 2019.
    Add Comment

    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.

    Answered on April 17, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.