如何获取tkinter中Canvas文本对象的大小?
我想在一个画布上创建一些文字:
myText = self.canvas.create_text(5, 5, anchor=NW, text="TEST")
现在我该怎么找到 myText
的宽度和高度呢?
相关问题:
2 个回答
4
这个方法看起来挺有效的,如果你只关心画布的宽度和高度的话。你可以用框的边界来计算,然后再检查差异,如果你想这样做的话,这种方法也同样有效。
width = myText.winfo_width()
height = myText.winfo_height()
22
bounds = self.canvas.bbox(myText) # returns a tuple like (x1, y1, x2, y2)
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
请查看这个 TkInter 参考资料。