1

I understand that it is possible to get the size of text on a canvas:

t = canvas.create_text(x, y, text='Hello StackOverflow')
print(canvas.bbox(t))

but I want to decide where to draw the text based on its size, so I need to know the size before the text is drawn. How to do that?

Louic
  • 2,403
  • 3
  • 19
  • 34
  • Duplicate question. See these questions and answers: [1](https://stackoverflow.com/questions/28883687/tkinter-label-change-font-size-by-text-length/28906292#28906292), [2](https://stackoverflow.com/questions/2922295/why-does-the-calculated-width-and-height-in-pixel-of-a-string-in-tkinter-differ), [3](https://stackoverflow.com/questions/48733568/displayof-in-tkinter-font-font-measure). – Sun Bear Nov 23 '18 at 21:27
  • I disagree. These are not duplicates. In my search these questions did not show up so I worded it differently. Also, this is text on a canvas and not a label, so it could have been different (but turned out it wasn't). – Louic Nov 23 '18 at 21:45
  • See these other previous questions and answers [1](https://stackoverflow.com/questions/7193798/how-can-i-measure-the-width-of-a-string-rendering-via-tkfont-without-creating-a), [2](https://stackoverflow.com/questions/30313518/how-can-i-make-tkinter-font-measurements-consistent-cross-platform), [3](https://stackoverflow.com/questions/26597886/python-tkinter-string-in-a-font-measures-differently-than-same-in-text-widgit-as), [4](https://stackoverflow.com/questions/30950925/tkinter-getting-screen-text-unit-width-not-pixels). Hope these can help you learn more about `Font.measure()`. ;) – Sun Bear Nov 24 '18 at 02:54
  • Thanks for the links. Still not duplicates though: the answers may be related but the questions were very different. Indeed it turned out later that the issue is related, but not a priori. – Louic Nov 24 '18 at 08:51

1 Answers1

2

A possible solution is

tk.font.Font(size=9, family='Helvetica').measure('HelloStackoverflow')
Louic
  • 2,403
  • 3
  • 19
  • 34