Tkinter Canvas Draw lines
Canvas within the tkinter module, this module can draw and animate.
Canvas objects can be created using the Canvas( ) method.
tk = Tk() # Use tk as a window Tk object
canvas = Canvas(tk, width=xx, height=yy) # xx, yy are the width and height of the canvas
canvas.pack() # You can pack the canvas well, which is necessary
After the canvas is created, the top left corner is the coordinate (0,0), incrementing to the right on the x-axis and incrementing down on the y-axis.
Draw lines create_line() is used in the following way.
create_line(x1, y1, x2, y2, ... xn, yn, options)
The lines will be drawn along (x1,y1), (x2,y2), …, the following are the common usage of options.
(1) arrow
: default is no arrows, use arrow=tk.FIRST to have arrows at the end of the start line, arrow=LAST to have arrows at the end of the last line, use arrow=tk.
(2) arrowshape
: use tuple (d1, d2, d3) to represent arrows, default is (8,10,3).
(3) capstyle
: this is the style of the end of the line, the default is BUTT, you can also choose PROJECTING, ROUND.
(4) dash
: create a dashed line, use a tuple to store numeric data, the first number is a solid line, the second number is blank, and so on when all the tuple numbers are used up and start again. For example, dash=(5,3) creates a 5 pixel solid line and a 3 pixel blank line, and so on. For example, dash=(8,1,1,1) produces 8 pixel solid lines and dotted lines, dash=(5,) produces 5 pixel solid lines and 5 pixel blanks.
(5) dashoffset
: produces the same dashed line as dash, but the number is the width of the blank at the beginning.
(6) fill
: set the line color.
(7) joinstyle
: set the intersection of lines, the default is ROUND, you can also choose BEVEL, MITER.
(8) stipple
: draw bitmap lines.
(9) width
: width of the line.
Example#1
Create 12 points on the periphery of a circle of radius 100 and connect these points to each other.
from tkinter import *
import math
tk = Tk()
canvas = Canvas(tk, width=640, height=480)
canvas.pack()
x_center, y_center, r = 320, 240, 100
x, y = [], []
for i in range(12): # Create 12 points on the periphery of a circle
x.append(x_center + r * math.cos(30*i*math.pi/180)) # x = x0 + r * cos(A)
y.append(y_center + r * math.sin(30*i*math.pi/180)) # y = y0 + r * sin(A)
for i in range(12): # Connect 12 points to each other
for j in range(12):
canvas.create_line(x[i],y[i],x[j],y[j])
tk.mainloop()
Output:
The above program uses the math functions sin() and cos() and pi, which are in the math module. When using create_line( ), you can set the line color with fill and the line width with width in the options parameter field.
Example#2
Different line color and width.
from tkinter import *
import math
tk = Tk()
tk.title("apidemos.com")
canvas = Canvas(tk, width=640, height=480)
canvas.pack()
canvas.create_line(100,100,500,100)
canvas.create_line(100,125,500,125,width=5)
canvas.create_line(100,150,500,150,width=10,fill='blue')
canvas.create_line(100,175,500,175,dash=(10,2,2,2))
tk.mainloop()
Output:
Example#3
Learn about the application of joinstyle parameter by line handover.
from tkinter import *
import math
tk = Tk()
tk.title("apidemos.com")
canvas = Canvas(tk, width=640, height=480)
canvas.pack()
canvas.create_line(30,30,500,30,265,100,30,30,
width=20,joinstyle=ROUND)
canvas.create_line(30,130,500,130,265,200,30,130,
width=20,joinstyle=BEVEL)
canvas.create_line(30,230,500,230,265,300,30,230,
width=20,joinstyle=MITER)
tk.mainloop()
Output:
Example#4
Understanding the application of the capstyle parameter by lines.
from tkinter import *
import math
tk = Tk()
tk.title("apidemos.com")
canvas = Canvas(tk, width=640, height=480)
canvas.pack()
canvas.create_line(30,30,500,30,width=10,capstyle=BUTT)
canvas.create_line(30,130,500,130,width=10,capstyle=ROUND)
canvas.create_line(30,230,500,230,width=10,capstyle=PROJECTING)
# Here is the vertical line
canvas.create_line(40,20,40,240)
canvas.create_line(500,20,500,250)
tk.mainloop()
Output:
Example#5
Create bitmap lines.
from tkinter import *
import math
tk = Tk()
tk.title("apidemos.com")
canvas = Canvas(tk, width=640, height=480)
canvas.pack()
canvas.create_line(30,30,500,30,width=50,stipple="gray25")
canvas.create_line(30,130,500,130,width=50,stipple="questhead")
canvas.create_line(30,230,500,230,width=50,stipple="info")
tk.mainloop()
Output: