如何在Python/Tkinter中找到画布项的大小?

2024-04-24 12:34:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在画布上创建一些文本:

myText = self.canvas.create_text(5, 5, anchor=NW, text="TEST")

现在我如何找到我的文本的宽度和高度?


Tags: texttest文本self宽度高度画布create
2条回答

如果您只关心所考虑的画布的宽度和高度,使用框的边界,然后检查差分效果,如果您想这样做的话,这个方法似乎可以很好地工作。

width = myText.winfo_width()  
height = myText.winfo_height()
bounds = self.canvas.bbox(myText)  # returns a tuple like (x1, y1, x2, y2)
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]

请参阅TkInter reference

相关问题 更多 >