Tkinter label height to fit content

Bart Kastermans bkasterm at gmail.com
Sun Sep 4 00:15:18 CEST 2011


I have a label into which I am going to put content of different
sizes. I would like to know how high I need to make the label so that I
can size the window so it can stay the same for the different content
sizes. I have a strategy, but it seems more complicated then it should
be.

I want to set a label to a given width and wraplength:

l = Label(root)
l['width'] = 30
l['wraplength'] = 244
l['text'] = "testing this"

Now I want to query the label to find how many lines are
used. l['height'] stays at 0, so the best I have been able to come up
with is to use l.winfo_height() and convert the height given in pixels
to the number of lines used. Nothing in dir(l) seems to give me the
information directly, but this strategy is fragile to font changes and
other changes.

Any suggestions?



More information about the Python-list mailing list