在 Tkinter 画布上如何绘制虚线?


要在 Tkinter 画布上绘制虚线,可以使用 create_line() 方法的 dash 参数。

步骤 -

  • 导入 tkinter 库并创建一个 tkinter 框架实例。

  • 使用 geometry 方法设置框架的大小。

  • 创建一个 Canvas 组件,并设置其 heightwidth

  • 接下来,使用 create_line 函数并传递线的坐标 (x1, y1) 和 (x2, y2)。

  • 若要获得虚线,请使用 dash 参数 dash=(5,1),表示 5 像素虚线后留 1 像素间距。

  • 可以使用 fillwidth 参数设置虚线的颜色和宽度。

  • 最后,运行应用程序窗口的 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()

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

它会生成以下输出 -

注意:虚线模式因系统而异。您可能在 Windows 和 Linux 系统上获得不同的输出。Windows 不支持与 Linux 相同的虚线模式。

更新日期: 26-Oct-2021

4K+ 浏览量

开启你的 职业生涯

通过完成课程来取得认证

开始
广告