Widget Canvas เป็น widget ยอดนิยมสำหรับการแสดงผลทางกราฟิกใน Tkinter แสดงเส้นใน Canvas ใช้เมธอดในตัว create_line(x1,y1,x2,y2, **options)
ระบุประเภทเส้นด้วยคุณสมบัติ dash เพื่อเปลี่ยนประเภทเส้นจากทึบเป็น dash แบบไดนามิก ใช้ configure() ส่งค่าว่างให้ dash จะเปลี่ยนจาก solid เป็น dash
ตัวอย่าง
ตัวอย่างการทำงาน
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the tkinter window
win.geometry("700x350")
def update_line():
canvas.itemconfig(line, dash=())
# Create a canvas widget
canvas=Canvas(win, width=400, height=300)
canvas.pack()
# Create a line
canvas.create_line(300, 30, 300, 150, dash=(4, 2), width=5)
# create a button to change the dash property of the line
ttk.Button(win, text="Change", command=update_line)
win.mainloop()
ผลลัพธ์
รันโค้ด จะแสดงเส้นประภายใน Canvas widget
