如何在 Tkinter 画布上绘制一条线?
Tkinter Canvas 组件可用于多种用途,例如绘制形状、对象、创建图形和图像。若要在 Canvas 上绘制一条线,我们可以使用 create_line(x,y,x1,y1, **options) 方法。
在 Tkinter 中,我们可以绘制两种类型的线:简单线和虚线。我们可以使用 dash 属性指定线的类型。
示例
# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win=Tk() # Set the size of the tkinter window win.geometry("700x350") # Create a canvas widget canvas=Canvas(win, width=500, height=300) canvas.pack() # Add a line in canvas widget canvas.create_line(100,200,200,35, fill="green", width=5) win.mainloop()
输出
运行上述代码将在 Canvas 组件上显示一条线。
广告