在 Tkinter 画布上如何绘制虚线?
要在 Tkinter 画布上绘制虚线,可以使用 create_line() 方法的 dash 参数。
步骤 -
导入 tkinter 库并创建一个 tkinter 框架实例。
使用 geometry 方法设置框架的大小。
创建一个 Canvas 组件,并设置其 height 和 width。
接下来,使用 create_line 函数并传递线的坐标 (x1, y1) 和 (x2, y2)。
若要获得虚线,请使用 dash 参数 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 相同的虚线模式。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP