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,080 points

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):

answered Dec 24, 2018 by Nymeria
• 3,520 points

can I add widgets to it after using this code 


What I did is, I created a button, if it is clicked it creates a new screen(basically just deletes everything off the canvas), can I add widgets to that blank canvas?

Hi, @There,

Yes, you can add widgets,

To display things on the canvas, you create one or more canvas items, which are placed in a stack. By default, new items are drawn on top of items already on the canvas.

Tkinter provides lots of methods allowing you to manipulate the items in various ways. Among other things, you can attach (bind) event callbacks to individual canvas items.

To draw things in the canvas, use the create methods to add new items.

from Tkinter import *

master = Tk()

w = Canvas(master, width=200, height=100)

w.create_line(0, 0, 200, 100)
w.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))

w.create_rectangle(50, 25, 150, 75, fill="blue")


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 coordsitemconfig, and move to modify the items or use delete to remove them.

i = w.create_line(xy, fill="red")

w.coords(i, new_xy) # change coordinates
w.itemconfig(i, fill="blue") # change color

w.delete(i) # remove

w.delete(ALL) # remove all items

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 aryya
• 7,370 points
0 votes
1 answer

How can I write code to find a palindrome in python without using string functions?

In Logic 1, try if i<int(length/2): instead of if i<int((length/2+1)): In ...READ MORE

answered Jan 16, 2019 in Python by charlie_brown
• 7,780 points
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, 2019 in Python by Mohammad
• 3,210 points
+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,780 points
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,213 views
0 votes
0 answers
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,520 points

edited Dec 12, 2018 by Nymeria 104 views