How do I find out the size of a canvas item in Python/Tkinter?
-
02-07-2019 - |
Question
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?
Solution
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.
OTHER TIPS
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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow