How can I measure the width of a string rendered via tkFont without first creating a window?

I can measure the text using tkFont, but I don't want the root window → tk.Tk ()

+4
source share
2 answers

You wanted to ask: "How to measure the width of a string rendering through tkFont without first creating a window?"

Answer: you cannot. Tk needs a root instance for drawing, etc.

you can create it, measure your text and immediately delete it with .delete() . It is so fast that the window does not appear to me.

+3
source

Now i have it and it works

 root = tk.Tk() font = tkFont.Font(family=fn, size=fs) w, h = (font.measure(text), font.metrics("linespace")) root.destroy() 
+5
source

Source: https://habr.com/ru/post/1368987/


All Articles