I want to create some text in a canvas:
我想在画布中创建一些文本:
myText = self.canvas.create_text(5, 5, anchor=NW, text="TEST")
Now how do I find the width and height of myText?
现在我如何找到myText的宽度和高度?
2 个解决方案
#1
10
bounds = self.canvas.bbox(myText) # returns a tuple like (x1, y1, x2, y2)
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
See the TkInter reference.
请参阅TkInter参考。
#2
6
This method seemed to work well if all you are interested in is the width and height of the canvas being considered, using the bounds of the box and then checking the differential works just as well if you want to do it that way.
如果您感兴趣的是所有您感兴趣的画布的宽度和高度,使用框的边界然后检查差异工作,如果您想这样做,这个方法似乎运行良好。
width = myText.winfo_width()
height = myText.winfo_height()
#1
10
bounds = self.canvas.bbox(myText) # returns a tuple like (x1, y1, x2, y2)
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
See the TkInter reference.
请参阅TkInter参考。
#2
6
This method seemed to work well if all you are interested in is the width and height of the canvas being considered, using the bounds of the box and then checking the differential works just as well if you want to do it that way.
如果您感兴趣的是所有您感兴趣的画布的宽度和高度,使用框的边界然后检查差异工作,如果您想这样做,这个方法似乎运行良好。
width = myText.winfo_width()
height = myText.winfo_height()