4

I can do the measure of text with tkFont, but I don't want a root window --> tk.Tk()

2 Answers 2

5

Now I have this, and it works

root = tk.Tk()
font = tkFont.Font(family=fn, size=fs)
w, h = (font.measure(text), font.metrics("linespace"))
root.destroy()
Sign up to request clarification or add additional context in comments.

Comments

3

You did mean to ask: “How can I measure the width of a string rendering via tkFont without creating a window first?”

And the answer is: you can’t. Tk needs its root instance to do drawing and such.

you can however create it, measure your text, and immediately dispose of it via .delete(). When doing that, the window doesn’t appear for me.

3 Comments

No problem :D i hope i’m right, though, and there isn’t a fancy way to do it. (or actually i hope i’m wrong and you get what you wanted. i doubt it, though). I edited the answer, btw.
A few lines of sample code with a string "THIS is a String" and the printed output with width would be helpful for the font and pixel width challenged like me :)
@WinEunuuchs2Unix I wrote this 13 years ago and haven’t touched Tk in the mean time, so feel free to do that yourself ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.