Drawing Lines using Tkinter – Basics for Beginners

Drawing Different Types Of Lines In Tkinter

In this article, we’ll see how to draw different types of lines in Tkinter. All the methods for drawing any shape or line are in the canvas class, so we’ll first initialize the Tkinter and canvas class.

Also read: Tkinter GUI Widgets – A Complete Reference

Importing and Initializing Module

Let’s begin by importing the required libraries and setting the basic window up. This will serve as a space for the below demonstrations.

from tkinter import *
root = Tk()
canvas = Canvas()


How To Draw Lines Using Tkinter?

For creating lines on our main Tkinter window we’ll use the create_line() method which takes the coordinates for line placement on the window. These coordinates decide the length and orientation of the line.

1. Straight Line

Creating any type of line is pretty easy in Tkinter. For drawing a straight we’ll use the create_line() method.

canvas.create_line(15, 25, 200, 25, width=5)
Straight Line In Tkinter
Straight Line In Tkinter

2. Dotted Line

The procedure of creating a dotted line is the same as the straight line. Similarly, we’ll use the create_line() method and pass the line coordinate, the only change is that we’ll also add another parameter dash.

canvas.create_line(300, 35, 300, 200, dash=(10), width=5)
Dotted Line In Tkinter
Dotted Line In Tkinter

3. Drawing Shapes With Multiple Lines

As we’ve discussed, we can also control the orientation of the lines which enables us to draw different shapes by creating multiple lines. In the given code we’ve taken 3 coordinates of three lines in such a way that it forms a triangle.

canvas.create_line(55, 85, 155, 85, 105, 180, 55, 85, width=5)
Shapes Using Lines In Tkinter
Shapes Using Lines In Tkinter


That’s it for the basics of drawing lines in Python Tkinter. To learn more about drawing lines, explore the create_line() function and the parameters that it accepts. It won’t be difficult to play around and create any type of drawings using this once you figure this function out!