Updated 2010-09-20 18:03:17 by Lihtox

label - create and manipulate label (text or image) widgets

Tk's label widget documentation: http://www.purl.org/tcl/home/man/tcl/TkCmd/label.htm

Ttk's themed label widget documentation: http://www.tcl.tk/man/tcl/TkCmd/ttk_label.htm

[INSERT normal examples of label use here]

For a multiline, label-like widget that supports wrapping, see message. Note that labels can show multiple lines, if their text includes newlines; they even have the -justify option.

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}

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... (RS)

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 c.l.t: 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.

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 April 20th - A small proc that I wrote and thought I'd sure, 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

See also

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.

Category Widget - Category Command - Tk syntax help - Arts and Crafts of Tcl-Tk Programming