如何重新配置 Tkinter 畫布項?


使用 Canvas 控制元件,我們可以建立文字、影像、圖形和視覺內容並新增到 Canvas 控制元件中。如果你需要動態配置 Canvas 項,那麼 tkinter 提供了 itemconfig(**options) 方法。你可以使用此方法配置 Canvas 項的屬性和屬性。例如,如果我們在 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()

輸出

如果你執行上述程式碼,它將顯示一個視窗,視窗中有一個按鈕和一條畫布上的線。

現在,單擊按鈕以更改畫布項的顏色。

更新於: 2021 年 8 月 6 日

9K+ 瀏覽量

啟動你的 職業

完成本課程,獲得認證

入門
廣告
© . All rights reserved.