Version 14 of label

Updated 2003-02-21 10:49:11

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

http://www.purl.org/tcl/home/man/tcl8.4/TkCmd/label.htm


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.


Tk syntax help - Arts and crafts of Tcl-Tk programming - Category Command