如何在Tkinter画布上绘制虚线?
要在Tkinter画布上绘制虚线,可以使用 create_line() 方法的dash参数。
步骤 −
- 导入tkinter库并创建一个tkinter框架实例。
-
使用 geometry 方法设置框架的大小。
-
创建一个画布小部件并设置其高度和宽度。
-
接下来,使用create_line函数并传递线的坐标(x1, y1)和(x2, y2)。
-
要获得虚线,请使用 dash=(5,1) 参数,表示5像素的虚线后跟1像素的空格。
-
您可以使用fill和width参数设置虚线的颜色和宽度。
-
最后,运行应用程序窗口的 mainloop 。
示例
# Import the library
from tkinter import *
# Create an instance of window
win = Tk()
# Set the geometry of the window
win.geometry("700x350")
C1 = Canvas(win, width=600, height=400)
# Coordinates of the line
coordinates = 100,150,550,150
# Draw a dashed vertical line, 5px dash and 1px space
C1.create_line(coordinates, dash=(5,1))
C1.pack()
win.mainloop()
输出
它将产生以下输出-
注意 :虚线图案取决于系统。您可能会在基于Windows和Linux的系统上获得不同的输出。Windows不支持与Linux相同的划线图案。