Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6537947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:40:37+00:00 2026-05-25T10:40:37+00:00

I have a label into which I am going to put content of different

  • 0

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?

Update: using Brian Oakley’s suggestion (which is similar to what I got on usenet) I have the following approximation to a solution (needs polishing, e.g. doesn’t take into account that Label breaks at whitespace):

import Tkinter as Tk
import tkFont
import random
import sys

def genstr (j):
    rno = random.randint(4,50)
    ret_val = str(j) + ":"
    for i in range (0, rno):
        ret_val += "hello" + str(i)
    return ret_val

def gendata (lh):
    ret_val = []
    for i in range(0,lh):
        ret_val.append (genstr (i))
    return ret_val

data = gendata (100)

root = Tk.Tk()
font = tkFont.Font(family='times', size=13)

class lines:
    def __init__ (self):
        self.lastct = 1   # remember where the cutoff was last work from there

    def count (self, text, cutoff = 400):
        global font
        no_lines = 1
        start_idx = 0
        idx = self.lastct

        while True:
            if idx > len (text):
                idx = len (text)

            # shrink from guessed value
            while font.measure (text[start_idx:idx - 1]) > cutoff:
                if idx <= start_idx:
                    print "error"
                    sys.exit ()
                else:
                    idx -= 1
                    self.lastct = idx - start_idx # adjust since was too big

            # increase from guessed value (note: if first shrunk then done)
            while (idx < len (text)
                   and font.measure (text[start_idx:idx]) < cutoff):
                idx += 1
                self.lastct = idx - start_idx     # adjust since was too small

            # next line has been determined
            print "*" + text[start_idx:idx-1] + "*"
            if idx == len(text) and font.measure (text[start_idx:]) < cutoff:
                return no_lines
            elif idx == len(text):
                return no_lines + 1
            else:
                no_lines += 1
                start_idx = idx - 1
                idx = start_idx + self.lastct

lin = lines()

for i in range(0,len(data)):
    lin.count(data[i], 450)

for i in range(0,min(len(data),10)):
    l = Tk.Label(root)
    l.pack()
    l['text'] = data[i]
    print i
    no = lin.count (data[i], 450)
    print "computed lines", no
    l['width'] = 50
    l['justify'] = Tk.LEFT
    l['anchor'] = 'w'
    l['wraplength'] = 450
    l['padx']=10
    l['pady'] = 5
    l['height'] = no
    l['font'] = font
    if i % 2 == 0:
        l['background'] = 'grey80'
    else:
        l['background'] = 'grey70'

root.mainloop()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:40:38+00:00Added an answer on May 25, 2026 at 10:40 am

    You are correct that the height attribute doesn’t change. That attribute doesn’t tell you the actual height, only the height it is configured to be. The actual height depends on factors such as how much text is in it, the wrap length, the font, and how the widget geometry is managed.

    tkinter Font objects have a measure method which lets you determine how tall and wide a string is for a given font. You can get the font for the widget and use that method to determine how much space is required for your string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a label which would display strings from 0 ft to say 999
I have put a label into my frame, but it refuses to move. SetBounds()
I have a label that holds a value and a text box into which
I have a label which labels the line numbers based on the text on
I have a Label, whose content is displayed by a while loop. when i
I have some problems inserting a const 1 into a textbox which is gridview.
I have to write app, which can scale font's size of label and buttons
i'm learning programming in C# and would like to know if it is possible
I have a code pattern which translates one integer to another. Just like this:
When you have a TableLayoutPanel on your Form and you drag a Label into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.