paxdiablo
paxdiablo

Reputation: 881543

TkInter get width of specific text

Is there a way to find out what width a Label would be for a given text and font, without having to add it to the graphical elements currently being displayed.

I need to get the information without any effect on the GUI, however momentary that effect may be. And, in any case, packing appears to work in a top-down fashion, meaning that controls may expand to fill others higher in the hierarchy but I can't get them to adjust those higher to allow for their actual size.

I'm more used to QML where I can just give an off-screen element the text and font details then just read out the bounding box.

Does anyone know how to do something similar in Tkinter?

Upvotes: 0

Views: 170

Answers (1)

acw1668
acw1668

Reputation: 46688

You can create the Label without calling pack() / grid() / place(), then call winfo_reqwidth() and winfo_reqheight() on the label to get its size.

Upvotes: 2

Related Questions