首页 > 解决方案 > tkinter 画布矩形使用项目配置调整大小

问题描述

我有一个画布并绘制了一个矩形,稍后我想根据变化的数据调整它的大小。

rect = canvas.create_rectangle(10, 130, 80, 20)

对于颜色,看起来有一个“填充”属性,但不确定我们如何更改坐标,尤其是高度。

canvas.itemconfigure(rect, fill="#000000", ...want to change height...)

能否请你帮忙

标签: tkinter

解决方案


您可以使用canvas.coords(...)来调整矩形的大小:

def resize_rect(dy):
    # get the current geometry of rectangle
    coords = canvas.coords(rect)
    # update its height by modifying the last value of coords
    coords[-1] += dy
    # update the rectangle
    canvas.coords(rect, coords)

推荐阅读