How to draw a dashed line on a Tkinter canvas?

To draw a dashed line on a Tkinter canvas, we can use the dash parameter of the create_line() method. The dash parameter accepts a tuple that defines the pattern of dashes and spaces.

Steps

  • Import the tkinter library and create an instance of tkinter frame.

  • Set the size of the frame using geometry() method.

  • Create a Canvas widget and set its height and width.

  • Use the create_line() function and pass the coordinates of the line (x1, y1) and (x2, y2).

  • To get a dashed line, use the dash parameter like dash=(5,1) for 5px dash followed by 1px space.

  • Set the color and width of the dashed lines using the fill and width parameters.

  • Finally, run the mainloop() of the application window.

Basic Example

Here's a simple example showing how to draw a horizontal dashed line ?

# Import the library
from tkinter import *

# Create an instance of window
win = Tk()

# Set the geometry of the window
win.geometry("700x350")

# Create canvas
canvas = Canvas(win, width=600, height=400, bg='white')

# Draw a horizontal dashed line
canvas.create_line(100, 150, 550, 150, dash=(5, 1), width=2, fill='black')

canvas.pack()
win.mainloop()

Different Dash Patterns

You can create various dash patterns by changing the tuple values ?

from tkinter import *

win = Tk()
win.geometry("700x400")

canvas = Canvas(win, width=600, height=350, bg='white')

# Different dash patterns
canvas.create_line(50, 50, 550, 50, dash=(10, 5), width=3, fill='red')     # Long dash, medium space
canvas.create_line(50, 100, 550, 100, dash=(5, 5), width=3, fill='blue')   # Equal dash and space
canvas.create_line(50, 150, 550, 150, dash=(2, 2), width=3, fill='green')  # Short dash, short space
canvas.create_line(50, 200, 550, 200, dash=(15, 3, 5, 3), width=3, fill='purple')  # Complex pattern

# Add labels
canvas.create_text(50, 30, text="dash=(10, 5)", anchor='w', font=('Arial', 10))
canvas.create_text(50, 80, text="dash=(5, 5)", anchor='w', font=('Arial', 10))
canvas.create_text(50, 130, text="dash=(2, 2)", anchor='w', font=('Arial', 10))
canvas.create_text(50, 180, text="dash=(15, 3, 5, 3)", anchor='w', font=('Arial', 10))

canvas.pack()
win.mainloop()

Vertical and Diagonal Dashed Lines

Dashed lines work for any direction ?

from tkinter import *

win = Tk()
win.geometry("600x500")

canvas = Canvas(win, width=550, height=450, bg='white')

# Vertical dashed line
canvas.create_line(150, 50, 150, 400, dash=(8, 3), width=2, fill='blue')

# Diagonal dashed line
canvas.create_line(200, 50, 500, 350, dash=(10, 5), width=2, fill='red')

# Horizontal dashed line
canvas.create_line(50, 200, 500, 200, dash=(6, 4), width=2, fill='green')

canvas.pack()
win.mainloop()

Dash Pattern Parameters

Pattern Description Example
dash=(5, 1) 5px dash, 1px space Short dashes
dash=(10, 5) 10px dash, 5px space Medium dashes
dash=(15, 10, 5, 10) Complex pattern Long dash, space, short dash, space

Note: Dash patterns are system-dependent. You may get different outputs on Windows and Linux based systems. Windows doesn't support the same dash patterns as Linux.

Conclusion

Use the dash parameter in create_line() to create dashed lines with custom patterns. The tuple values control the length of dashes and spaces, allowing for various visual effects in your Tkinter applications.

Updated on: 2026-03-26T18:34:21+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements