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,060 points
1,098 views

1 answer to this question.

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
92 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 May 20 in Python by Mohammad
• 1,500 points
23 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
1,096 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 128 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 36 views