How to wipe Tkinter Canvas with Python?

169    Asked by Dannasahi in Python , Asked on Jul 27, 2023

On drawing a shape by the below-mentioned code:

canvas.create_rectangle(10, 10, 50, 50, color= “green”)

Does Tkinter remember that it was made?

I am creating a code having one Frame making few rectangles. Then I draw a black rectangle to erase the screen, and draw few rectangles. I just draw the shape directly in the canvas, so does it stay in memory?

Answered by Diana Campbell

The delete method can be utilized to clear Tkinter canvas with Python.

The method guarantees that memory leaks are evaded and millions of objects are not made. Pass “all” to erase every item in the canvas.

canvas.delete(“all”)
“All” refers to a unique tag depicting the items on the canvas.
The following code will simplify the thing:
#import the tkinter library
From tkinter import *
#create an instance of tkinter frame
Win = Tk()
#set the geometry
win.geometry(“650x250”)
#creating a canvas
Mycanvas =canvas(win, bg=”white”, height= 2–, width= 2–)
coordinates= 10, 10, 200, 200
Arc = mycanvas.create_arc(corodinates, start=0, extent=32-, fill=”red”)
mycanvas.pack9)
#clearing the canvas
mycanvas.delete(‘all’)
win.mainloop()

This code will erase the canvas.

The Python online training offered at JanBask Training gives you an experience like offline classes thereby saving the students from the tiring journey to travel to the physical locations. The course provides a total Python discipline’s preparation by touching on every core concept from basic to advanced thereby making your job ready to face the tough competitive market.



Your Answer

Interviews

Parent Categories