4

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

Alquimista
  • 862
  • 2
  • 10
  • 24

2 Answers2

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()
Alquimista
  • 862
  • 2
  • 10
  • 24
3

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.

flying sheep
  • 8,475
  • 5
  • 56
  • 73
  • 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. – flying sheep Aug 26 '11 at 19:28