如何重新配置 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.