How to draw a line on a Tkinter canvas?


Tkinter Canvas widget can be used for multiple purposes such as drawing shapes, objects, creating graphics and images. To draw a line on a Canvas, we can use create_line(x,y,x1,y1, **options) method.

In Tkinter, we can draw two types of lines: simple and dashed. We can specify the type of line using the dash property.

Example

# 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")

# Create a canvas widget
canvas=Canvas(win, width=500, height=300)
canvas.pack()

# Add a line in canvas widget
canvas.create_line(100,200,200,35, fill="green", width=5)

win.mainloop()

Output

Running the above code will display a line on the Canvas widget.

Updated on: 06-Aug-2021

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements