问题描述
polygon = None
def polygon(event):
global polygon
polygon = my_canvas.create_polygon([150,75,225,300,150],outline='gray',fill='gray',width=2)
print(polygon)
def resizepolygon(event):
my_canvas.scale(poly,20,100,100)
my_canvas.pack()
resizepolygon 使多边形消失而不是调整多边形的大小,如何调整多边形的大小?
解决方法
您需要找到多边形边界框的中心 (x,y)
,并将它们用作 xOrigin
中的 yOrigin
和 .scale()
。
也最好使用较小的 xScale
和 yScale
来查看结果,100
有点大。
def resizePolygon(event):
# get the bounding box of the polygon
x1,y1,x2,y2 = my_canvas.bbox(polygon)
# calculate the center (x,y) in the bounding box
cx,cy = (x1+x2)/2,(y1+y2)/2
# scale the polygon
my_canvas.scale(polygon,cx,cy,10,10) # use scale 10 instead of 100 to see the effect
#my_canvas.pack()
,
<tkinter.Canvas>.scale(xscale,yscale,xoffset,yoffset)
来自here:
按比例因子调整匹配项的大小。每个项目的坐标重新计算为 ((coord-offset)*scale+offset);换句话说,每个项目首先移动-offset,然后乘以比例因子,然后再次移回。注意这个方法修改了item坐标;如果在同一项目上多次使用此方法,则可能会降低精度。
我认为您将多边形放大了 20 倍,并将其在 x 和 y 方向上移动了 1900 像素,因此它从屏幕上消失了。此外,该方法会调整整个画布而不是多边形的大小。
尝试使用:<tkinter.Canvas>.coords(object,*new_coords)