How to I clear Tkinter Canvas using Python?

0 votes

When I draw a shape using:

canvas.create_rectangle(15, 15, 60, 60, color="blue")

Does Tkinter keep track of the fact that it was created?

I am making a game where 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.

Am I creating thousands of rectangle objects in memory? What is the right way to do it? Appreciate some help here!

Dec 24, 2018 in Python by Anirudh
• 2,050 points
697 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

To clear a canvas, use the delete method. 

This ensures you avoid memory leaks and not end up creating thousands of objects.

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 Dec 24, 2018 by Nymeria
• 3,500 points

Related Questions In Python

0 votes
1 answer

How can I expose callbacks to Fortran using Python

The code that I've written below. The ...READ MORE

answered Aug 23, 2018 in Python by ariaholic
• 7,320 points
63 views
0 votes
1 answer
0 votes
1 answer

How can I write a program to add two numbers using functions in python?

def add(a,b):      return a + b #when i call ...READ MORE

answered 1 day ago in Python by Mohammad
• 1,400 points
7 views
+2 votes
2 answers

How to make a laplacian pyramid using OpenCV python?

down voteacceptTheeThe problem is that you're iterating ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,710 points
947 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
20 views
0 votes
1 answer

How to put a variable inside a String using Python?

In the easiest way, you can create ...READ MORE

answered Nov 23, 2018 in Python by Nymeria
• 3,500 points

edited Dec 12, 2018 by Nymeria 26 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.