label

label , a Tk command, creates and manipulate label (text or image) widgets

See Also

ttk::label
a ttk-themed label
label selection
label wrapping
labelled line
message
a multiline, label-like widget that supports wrapping.
vertical labels
label widget in Tix

Documentation

official reference

Description

labels can show multiple lines, if their text includes newlines; they even have the -justify option.

Composite Labels

Composite labels: A text label can hold either a constant text or a reference to a textvariable from which it is updated. Occasionally one wants both mixed, for instance to display "Distance from $from to $to: $mi miles", and auto-updated when one of those variables changes. The following code can help:

proc compositelabel {w contents} {
    frame $w
    set n 0
    foreach i $contents {
        set w1 [label $w.[incr n]]
        if [regexp {^[$](.+)} $i -> varname] {
            $w1 configure -textvar ::$varname
        } else {
            $w1 configure -text $i
        }
    }
    eval pack [winfo children $w] -side left
}
# usage example:
compositelabel .dist {Distance from $from to $to is $m miles}

RS: Make sure that the second argument is braced - the dollar signs are not to be seen by the Tcl parser, but rather used to indicate the following is the name of a global variable, not a text constant...

DKF: The other notion of composite label, that of a label that holds both picture and text, is also supported by Tk from 8.4 onwards.

Selectable labels

Bryan Oakley wrote in comp.lang.tcl: Here's a quick hack. Caveat emptor: this example has hard-coded color names that probably only work on windows. But if you are on unix (or even on windows...) pick any colors that you want:

bind Label <1> {focus %W}

bind Label <FocusIn> {
    %W configure -background SystemHighlight -foreground SystemHighlightText
}
bind Label <FocusOut> {
    %W configure -background SystemButtonFace -foreground SystemButtonText
}
bind Label <Control-c> {
    clipboard clear
    clipboard append [%W cget -text]
}

See also label selection.

Geometry

Among the benefits of Tk is that its programming model encourages "intelligent" displays, and specifically those with modestly dynamic labeling. Run-time context might call for a change in the text of a button (an alternation between "Stop" and "Re-start", for example) or label. The one technical problem that immediately arises with most such improvements is changes in widget size. Bryan Oakley posted a brief explanation of the usual idiom for these situations: "Put it in a frame, and force the frame to a specific height [or width] is one way of doing it. Read up on 'pack propagate', or 'grid propagate' to see how you can keep children from causing their parent[s] to resize."

Incidentally, BLT enjoys a wealth of geometry configurations which help with these tasks.


RS just specifies a sufficient width for labels with changing content, which turns off the resizing.


MG 2004-04-20: A small proc that I wrote and thought I'd share, although I don't think it has a huge range of uses. Given a label widget as its only argument, it works out from the -width option, and the -font being used in the widget, how many pixels the -wraplength needs to be to stop the text overflowing.

proc calcWrap {w} {
    set font [$w cget -font]
    set x [font measure $font -displayof $w [string repeat 0 [$w cget -width]]]
};# calcWrap

Focus

On Windows (Xp, 8), the -highlight... options don't seem to work. Schelte Bron postulated that this is because on Windows, the highlight is inside a widget instead of around it. One workaround is to manipulate -foreground or -background instead.


MGS: I have combined the code in label wrapping and label selection and created a package which you can find in the Links section at Mark G. Saye.