# Tkinter label height to fit content

Bart Kastermans bkasterm at gmail.com
Wed Sep 7 02:33:32 CEST 2011

```rantingrick <rantingrick at gmail.com> writes:

> On Sep 6, 5:00 pm, Bart Kastermans <bkast... at gmail.com> wrote:
>> rantingrick <rantingr... at gmail.com> writes:
>> > Hmm, i can replace all that code with this...
>>
>> Because I stupidly forgot to repeat the original problem I had, and my
>> code doesn't show it (and doesn't show the correct use of the function I
>> wrote).
>
> Oh NOW i see! This new code you posted is like night and day compared
> to the original </sarcasm> :o)

Quite, I should know better (I deal with students thinking what is in
their mind should be clear to me all the time):

##############################################
# run through all the data we have, compute the maximum height
max_ht = 0
for i in range(0,len(data)):
# lin.count is the line counting function
ct = lin.count(data[i], 450)
if ct > max_ht:
max_ht = ct

for i in range(0,min(len(data),5)):
# l is the Tkinter.Label with all formatting applied and data[i]
# set for text
l['height'] = max_ht    # use this maximum height for all Labels.

###############################################

Thinking on this some more, the first computation should surely be

max_ht = max (map (lambda x: lin.count(x, 450), data))

The second one could then be

labels = map (label_prepare_pack, data[:5])

where label_prepare_pack prepares (sets all attributes and data), packs,
and returns the new label.  The first (for max_ht) is a clear
improvement to me, the second (for labels) uses too many side-effects to
be very appealing to me.

> Anyway, i did not read the code to find the difference because i want
> to know why you insist on using multiple labels for this when a
> scrolled text will suffice. Are you trying to create some sort of
> textual "animation frames" that you can flip through on demand?

I don't follow precisely, but I am indeed looking to flip through all of
data while showing only 5 or 10 at a time.  The problem I wanted to
solve was that my window kept changing size while doing this.

> If not
> i would use the scrolled text (and even the scrolled text can "feel"
> like animation frames whist scrolling).
>
> Take your input data and replace ALL single newlines with null strings
> (thereby preserving paragraphs) and let the textbox control the line
> wrapping. The benefits are enormous using my way; your way is limited
> and requires re-inventing the wheel. Tell me WHY the textbox approach
> is not a viable solution and THEN i'll listen to you.

The reason I thought this, was that I didn't realize I could bind
actions to tags (to get different actions for different bits of text).
Now that I do know this I could use code like my lin.count to get the
maximum height still (to get a constant location for the i-th item as
the items are changed; more importantly to get a constant height for the
whole list of items), and then use tags to bind the corresponding
actions.

> Until then; you can lead a horse to water...

I certainly appreciate your trying.  I might not see the fresh stream
yet, but I do see liquid (possibly a shallow muddy pool, but big
progress from the dry sandy dunes before).  I will keep both approaches
in mind as I further develop.

Again, thanks for the help, greatly appreciated.

```