Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
The Canvas
widget in tkinter
provides the capability to draw lines in various styles. You can adjust the width, style, fill color, and even create dashed patterns.
In this tutorial, we'll demonstrate how to create different types of lines using the Canvas
class in tkinter
.
tkinter
:1. Import Required Libraries:
import tkinter as tk
2. Initialize Main Application Window:
root = tk.Tk() root.title("Canvas Lines Tutorial")
3. Create and Pack the Canvas Widget:
canvas = tk.Canvas(root, bg="white", width=400, height=400) canvas.pack(pady=20)
4. Drawing Different Lines:
The most basic type of line can be drawn with default settings:
canvas.create_line(10, 10, 200, 10)
You can set the width of a line using the width
option:
canvas.create_line(10, 30, 200, 30, width=5)
Use the fill
option to change the line's color:
canvas.create_line(10, 60, 200, 60, fill="red")
You can create dashed lines with the dash
option:
canvas.create_line(10, 90, 200, 90, dash=(5, 2)) # 5 pixels on, 2 pixels off
You can add arrowheads at either or both ends of a line:
canvas.create_line(10, 120, 200, 120, arrow=tk.LAST) # Arrow at the last end of the line canvas.create_line(10, 150, 200, 150, arrow=tk.BOTH) # Arrows at both ends
You can also customize the shape of the arrowhead:
canvas.create_line(10, 180, 200, 180, arrow=tk.LAST, arrowshape=(16, 20, 6))
5. Mainloop to Run the Application:
root.mainloop()
Complete Code:
import tkinter as tk root = tk.Tk() root.title("Canvas Lines Tutorial") canvas = tk.Canvas(root, bg="white", width=400, height=400) canvas.pack(pady=20) # Drawing different types of lines canvas.create_line(10, 10, 200, 10) canvas.create_line(10, 30, 200, 30, width=5) canvas.create_line(10, 60, 200, 60, fill="red") canvas.create_line(10, 90, 200, 90, dash=(5, 2)) canvas.create_line(10, 120, 200, 120, arrow=tk.LAST) canvas.create_line(10, 150, 200, 150, arrow=tk.BOTH) canvas.create_line(10, 180, 200, 180, arrow=tk.LAST, arrowshape=(16, 20, 6)) root.mainloop()
This code provides a demonstration of various line styles on the canvas. You can combine different options to achieve a wide range of line appearances.
Python Tkinter draw lines on Canvas example:
Canvas
widget allows you to draw lines using the create_line
method.import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=300, height=200) canvas.pack() # Draw a straight line canvas.create_line(50, 50, 200, 50, fill="blue") root.mainloop()
Creating straight and curved lines in Tkinter:
create_line
method.import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=300, height=200) canvas.pack() # Draw a straight line canvas.create_line(50, 50, 200, 50, fill="blue") # Draw a curved line canvas.create_line(50, 100, 200, 150, smooth=True, fill="red") root.mainloop()
Drawing dashed and dotted lines in Tkinter:
dash
option in the create_line
method.import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=300, height=200) canvas.pack() # Draw a dashed line canvas.create_line(50, 50, 200, 50, dash=(4, 2), fill="blue") # Draw a dotted line canvas.create_line(50, 100, 200, 100, dash=(1, 1), fill="red") root.mainloop()
Python Tkinter Canvas create_line example:
create_line
method in Tkinter's Canvas
widget is used to draw lines by specifying the coordinates of the endpoints.import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=300, height=200) canvas.pack() # Draw a line canvas.create_line(50, 50, 200, 50, fill="blue") root.mainloop()
Customizing line styles on Canvas in Tkinter:
width
, fill
, and dash
in the create_line
method.import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=300, height=200) canvas.pack() # Customize line styles canvas.create_line(50, 50, 200, 50, width=2, fill="blue", dash=(4, 2)) canvas.create_line(50, 100, 200, 100, width=3, fill="red", dash=(1, 1)) root.mainloop()
Creating arrows and connectors with Tkinter Canvas:
Canvas
widget allows you to create arrows and connectors using the arrow
option in the create_line
method.import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=300, height=200) canvas.pack() # Draw an arrow canvas.create_line(50, 50, 200, 50, arrow=tk.LAST, fill="blue") # Draw a connector canvas.create_line(50, 100, 200, 100, arrow=tk.BOTH, fill="red") root.mainloop()
Drawing polylines and multi-segment lines in Tkinter:
create_line
method with multiple coordinate pairs.import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=300, height=200) canvas.pack() # Draw a polyline canvas.create_line(50, 50, 100, 100, 150, 50, fill="blue") root.mainloop()
Python Tkinter Canvas line coordinates:
create_line
method, you specify the coordinates of the endpoints of the line as pairs of x and y values.import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=300, height=200) canvas.pack() # Draw a line with specified coordinates canvas.create_line(50, 50, 200, 50, fill="blue") root.mainloop()