我希望在画布上的矩形的轮廓获得更大的宽度,当矩形处于"禁用"状态时.因此,我使用参数"DisabledWidth=4".但当矩形处于"禁用"状态时,轮廓的宽度仍为1而不是4.

这是我的代码,它显示了问题:当我将鼠标移到矩形上时,矩形的状态更改为"活动",一切都按预期工作,特别是轮廓宽度更改为4.但是,当我通过单击按钮将状态更改为"禁用"时,轮廓保持在宽度1.我做错了什么?

import tkinter as tk
def disabled():
    canvas.itemconfig(rect, state="disabled")
def normal():
    canvas.itemconfig(rect, state="normal")
root    = tk.Tk()
canvas  = tk.Canvas(root, height=250, width=250)
button1 = tk.Button(root, text="change rectangle to state disabled", command=disabled)
button2 = tk.Button(root, text="change rectangle to state normal"  , command=normal  )
rect = canvas.create_rectangle(40, 40, 180, 180,
                fill           = "red",
                activefill     = "green2",
                activeoutline  = "green3",
                activewidth    = 4,
                disabledfill   = "grey",
                disabledoutline= "grey2",
                disabledwidth  = 4
                )
canvas.grid()
button1.grid()
button2.grid()
root.mainloop()

推荐答案

canvas.itemconfig为单位指定宽度

import tkinter as tk

def disabled():
    canvas.itemconfig(rect, state="disabled", width=4)

def normal():
    canvas.itemconfig(rect, state="normal", width=1)

root    = tk.Tk()
canvas  = tk.Canvas(root, height=250, width=250)
button1 = tk.Button(root, text="change rectangle to state disabled", command=disabled)
button2 = tk.Button(root, text="change rectangle to state normal"  , command=normal  )
rect = canvas.create_rectangle(40, 40, 180, 180,
                fill           = "red",
                activefill     = "green2",
                activeoutline  = "green3",
                activewidth    = 4,
                disabledfill   = "grey",
                disabledoutline= "grey2",
                width          = 1
                )
canvas.grid()
button1.grid()
button2.grid()
root.mainloop()

编辑:这似乎是一个真正的错误.我已经申请了report分.

Python相关问答推荐

Polars:使用列值引用when / then表达中的其他列

分组数据并删除重复数据

三个给定的坐标可以是矩形的点吗

DataFrame groupby函数从列返回数组而不是值

Pre—Commit MyPy无法禁用非错误消息

在单个对象中解析多个Python数据帧

如何从列表框中 Select 而不出错?

使用Python从rotowire中抓取MLB每日阵容

如何排除prefecture_related中查询集为空的实例?

如何防止Pandas将索引标为周期?

合并与拼接并举

如何在Python请求中组合多个适配器?

如何在FastAPI中替换Pydantic的constr,以便在BaseModel之外使用?'

极点替换值大于组内另一个极点数据帧的最大值

如果不使用. to_list()[0],我如何从一个pandas DataFrame中获取一个值?

如何提高Pandas DataFrame中随机列 Select 和分配的效率?

比较两个有条件的数据帧并删除所有不合格的数据帧

如何将一个文件的多列导入到Python中的同一数组中?

我怎样才能让深度测试在OpenGL中使用Python和PyGame呢?

无法使用请求模块从网页上抓取一些产品的名称