要在 Tkinter 画布上绘制虚线,我们可以使用create_line()方法的 dash 参数。
导入 tkinter 库并创建 tkinter 框架的实例。
使用几何方法设置框架的大小。
创建一个 Canvas 小部件并设置它的height和width。
接下来,使用 create_line 函数并传递线 (x1, y1) 和 (x2, y2) 的坐标。
要获得虚线,请使用破折号参数dash=(5,1)表示 5px 破折号,后跟 1px 空间。
您可以使用填充和宽度参数设置虚线的颜色和宽度。
最后,运行应用程序窗口的主循环。
# 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 相同的破折号模式。