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

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

现在如何找到myText的宽度和高度?

有帮助吗?

解决方案

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

请参阅 TkInter参考

其他提示

如果您感兴趣的是所有您感兴趣的画布的宽度和高度,使用框的边界然后检查差异工作,如果您想这样做,这个方法似乎运行良好。

width = myText.winfo_width()  
height = myText.winfo_height()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top