Tkinter – How to create colored lines based on length?

Tkinter Canvas widget is one of the versatile widgets which is generally used to draw shapes, arcs, objects, display images or any content. The objects inside the Canvas widget can be modified as well as configured using the configure() method or within the constructor by providing values to the properties.

To create lines on a Canvas widget, you can use the create_line(x1, y1, x2, y2, fill="color", width, **options) method. The coordinates x1, y1 define the starting point and x2, y2 define the ending point of the line. The distance between these points determines the length of the line.

Syntax

canvas.create_line(x1, y1, x2, y2, fill="color", width=thickness)

Parameters

  • x1, y1 ? Starting coordinates of the line
  • x2, y2 ? Ending coordinates of the line
  • fill ? Color of the line (hex code or color name)
  • width ? Thickness of the line in pixels

Example

Let's create three lines with different colors and lengths in the Canvas widget ?

# Import the tkinter library
from tkinter import *

# Create an instance of tkinter window
win = Tk()
win.geometry("700x350")
win.title("Colored Lines")

# Create a canvas widget
my_canvas = Canvas(win, width=400, height=400, background="yellow")
my_canvas.pack()

# Create colored lines with different lengths
my_canvas.create_line(20, 0, 400, 400, fill="#44a387", width=10)    # Long diagonal line
my_canvas.create_line(0, 0, 400, 300, fill="#a5a344", width=10)     # Medium diagonal line
my_canvas.create_line(0, 0, 400, 200, fill="#9d44a3", width=10)     # Short diagonal line

# Run the mainloop
win.mainloop()

Creating Lines with Calculated Lengths

You can also create lines by calculating specific lengths using mathematical functions ?

from tkinter import *
import math

win = Tk()
win.geometry("600x400")
win.title("Lines with Calculated Lengths")

canvas = Canvas(win, width=500, height=350, background="white")
canvas.pack()

# Create lines with specific calculated lengths
start_x, start_y = 50, 50

# Line 1: 100 pixels long at 45 degrees
length1 = 100
angle1 = math.radians(45)
end_x1 = start_x + length1 * math.cos(angle1)
end_y1 = start_y + length1 * math.sin(angle1)
canvas.create_line(start_x, start_y, end_x1, end_y1, fill="red", width=5)

# Line 2: 150 pixels long at 30 degrees
length2 = 150
angle2 = math.radians(30)
end_x2 = start_x + length2 * math.cos(angle2)
end_y2 = start_y + length2 * math.sin(angle2)
canvas.create_line(start_x, start_y, end_x2, end_y2, fill="blue", width=5)

# Line 3: 200 pixels long horizontally
canvas.create_line(start_x, start_y + 100, start_x + 200, start_y + 100, fill="green", width=5)

win.mainloop()

Color Mapping Based on Length

Here's an example that automatically assigns colors based on line length ?

from tkinter import *

def get_color_by_length(length):
    """Return color based on line length"""
    if length < 100:
        return "red"
    elif length < 200:
        return "orange"
    elif length < 300:
        return "green"
    else:
        return "blue"

win = Tk()
win.geometry("600x400")
win.title("Color-coded Lines by Length")

canvas = Canvas(win, width=500, height=350, background="lightgray")
canvas.pack()

# Create lines with different lengths and auto-assigned colors
lines_data = [
    (50, 50, 120, 50),    # Length: 70 pixels
    (50, 100, 200, 100),  # Length: 150 pixels
    (50, 150, 300, 150),  # Length: 250 pixels
    (50, 200, 400, 200),  # Length: 350 pixels
]

for x1, y1, x2, y2 in lines_data:
    length = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
    color = get_color_by_length(length)
    canvas.create_line(x1, y1, x2, y2, fill=color, width=8)
    
    # Add length label
    canvas.create_text(x1 - 30, y1, text=f"{int(length)}px", fill="black")

win.mainloop()

Output

Running any of the above examples will display colored lines in the Canvas widget. The lines will have different colors, lengths, and positions based on the coordinates provided.

Colored Lines with Different Lengths

Key Points

  • Line length is determined by the distance between start and end coordinates
  • Use the fill parameter to set line color
  • The width parameter controls line thickness
  • Colors can be hex codes (#44a387) or color names (red, blue)
  • You can calculate line lengths using mathematical formulas

Conclusion

Creating colored lines based on length in Tkinter involves using the create_line() method with appropriate coordinates and styling parameters. You can either manually set colors for different lengths or programmatically assign colors based on calculated line lengths.

Updated on: 2026-03-26T18:51:12+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements