- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete lines from a Python tkinter canvas?
The Canvas widget has many use-cases in GUI application development. We can use a Canvas widget to draw shapes, creating graphics, images, and many other things. To draw a line in Canvas, we can use create_line(x,y,x1,y1, **options) method. In Tkinter, we can draw two types of lines − simple and dashed.
If you want your application to delete the created lines, then you can use the delete() method.
Example
Let us have a look at the example where we will delete the line defined in the Canvas widget.
# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win=Tk() # Set the size of the tkinter window win.geometry("700x350") # Define a function to delete the shape def on_click(): canvas.delete(line) # Create a canvas widget canvas=Canvas(win, width=500, height=300) canvas.pack() # Add a line in canvas widget line=canvas.create_line(100,200,200,35, fill="red", width=10) # Create a button to delete the button Button(win, text="Delete Shape", command=on_click).pack() win.mainloop()
Output
If we run the above code, it will display a window with a button and a shape in the canvas.
Now, click the "Delete Shape" button to delete the displayed line from the canvas.
- Related Articles
- How to delete Tkinter widgets from a window?
- How to draw a png image on a Python tkinter canvas?
- How to center an image in canvas Python Tkinter
- How to clear Tkinter Canvas?
- How to change the TKinter canvas line from dash to solid?
- How to move a Tkinter canvas with Mouse?
- How to make a Tkinter canvas rectangle transparent?
- How to add text inside a Tkinter Canvas?
- How to reconfigure Tkinter canvas items?
- How to create a Button on a Tkinter Canvas?
- How to draw a line on a Tkinter canvas?
- How to draw a dot on a canvas on a click event in Tkinter Python?
- How to update an image in a Tkinter Canvas?
- How to draw an arc on a tkinter canvas?
- How to draw lines using HTML5 Canvas?

Advertisements