Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีปรับค่า Canvas Items ใน Tkinter แบบไดนามิก


ใช้ Widget Canvas สร้างข้อความ, ภาพ, กราฟิก, และเนื้อหาทางสายตา เพิ่มลงใน Canvas หากต้องการตั้งค่า Canvas item แบบไดนามิก Tkinter มีเมธอด itemconfig(**options) ใช้เมธอดนี้ตั้งค่าคุณสมบัติและแอตทริบิวต์ของ Canvas items เช่น สร้างเส้นใน Canvas สามารถตั้งค่าสีหรือความกว้างด้วย itemconfig()

ตัวอย่าง

# 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") 
# Define a function to delete the shape 
def on_click(): 
    canvas.itemconfig(line, fill="green") 
# Create a canvas widget 
canvas = Canvas(win, width=500, height=300) 
canvas.pack() 
# Add a line in canvas widget 
line = canvas.create_line(100, 200, 200, 35, fill="yellow", width=5) 
# Create a button to delete the button 
Button(win, text="Update the Color", command=on_click).pack() 
win.mainloop()

ผลลัพธ์

รันโค้ด จะแสดงหน้าต่างพร้อมปุ่มและเส้นบน canvas

วิธีปรับค่า Canvas Items ใน Tkinter แบบไดนามิก

คลิกปุ่มเพื่อเปลี่ยนสีของ canvas item

วิธีปรับค่า Canvas Items ใน Tkinter แบบไดนามิก