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

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


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()


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

An the answer is: you can’t. Tk needs it’s root instance to do drawing and such.

you can however create it, measure your text, and immediately dispose it via .delete(). This is so fast that the window doesn’t appear for me.