[Tkinter-discuss] more control over truncating long strings in Labels?

Michael Lange klappnase at web.de
Wed Apr 27 22:37:02 CEST 2005

On Wed, 27 Apr 2005 11:33:49 -0700
"Russell E. Owen" <rowen at cesmail.net> wrote:

> I'd was wondering if there was some fairly easy way to display text in a 
> Label such that it is is anchored on the west (short strings are against 
> the left edge), but too-long strings are truncated on the left. This 
> would be useful for displaying very long path names. It might be even 
> nicer to truncate the text in the middle, replacing the excess text with 
> ..., but truncating on the left would be fine.
> I fear the only way is to use font metrics (using a tkFont object from 
> the tkFont module) and iteratively try to figure out what length of 
> string will fit. If so, it sounds messy. Hope I'm missing something.
> -- Russell

I've done something like that by simply checking the length of the string, like this:

label = Label(parent, width=40, anchor='w')
if len(labeltext) > 36:
    labeltext = '...%s' % labeltext[-33:]

Maybe it's not perfect, but for me it worked fine.

Best regards


More information about the Tkinter-discuss mailing list