Monday, November 18, 2024
Google search engine
HomeLanguagesHow to clear Tkinter Canvas?

How to clear Tkinter Canvas?

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




# import tkinter
from tkinter import *
  
# make an object of Tk interface
window = Tk()
  
# Give the title to out window
window.title('GFG')
  
# creating canvas
canvas = Canvas(window, width=300, height=200)
canvas.pack()
  
# draw line to out canvas
canvas.create_line(0, 0, 300, 200)
canvas.create_line(0, 200, 300, 0)
  
# draw oval to out canvas
canvas.create_oval(50, 25, 250, 175, fill="yellow")
  
window.mainloop()


Output:

Simple canvas Example

After clearing canvas

Python3




# import tkinter
from tkinter import *
  
# make an object of Tk interface
window = Tk()
  
# Give the title to out window
window.title('GFG')
  
# creating canvas
canvas = Canvas(window, width=300, height=200)
canvas.pack()
  
# draw line to out canvas
canvas.create_line(0, 0, 300, 200)
canvas.create_line(0, 200, 300, 0)
  
# draw oval to out canvas
canvas.create_oval(50, 25, 250, 175, fill="yellow")
  
# clear the canvas
canvas.delete('all')
  
window.mainloop()


Output:

Cleared canvas

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments