Tkinter is a Python Package for creating effective GUI applications. Tkinter’s Canvas widget is nothing but a rectangular area that is used for drawing pictures, simple shapes, or any complex graph. We can place any widgets like text, button, or frames on the canvas.
The task here is to generate a Python script that can clear Tkinter Canvas. For that delete function of this module will be employed. This method has a special parameter all which represents all the component on the canvas. To clear this canvas give this special parameter to the delete method. Thus, the line below is sufficient to clear the canvas:
delete('all')
If you want to delete any specific item then you can assign a tag to that item and instead of all pass that tag to the delete method.
Given below is the code to achieve this specific functionality:
Program:
Before clearing canvas
Python3
from tkinter import *
window = Tk()
window.title('GFG')
canvas = Canvas(window, width=300, height=200)
canvas.pack()
canvas.create_line(0, 0, 300, 200)
canvas.create_line(0, 200, 300, 0)
canvas.create_oval(50, 25, 250, 175, fill="yellow")
window.mainloop()
|
Output:

Simple canvas Example
After clearing canvas
Python3
from tkinter import *
window = Tk()
window.title('GFG')
canvas = Canvas(window, width=300, height=200)
canvas.pack()
canvas.create_line(0, 0, 300, 200)
canvas.create_line(0, 200, 300, 0)
canvas.create_oval(50, 25, 250, 175, fill="yellow")
canvas.delete('all')
window.mainloop()
|
Output:

Cleared canvas
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated :
11 Dec, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...