Version 9 of label

Updated 2002-01-24 15:58:13

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)


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 parents to resize."


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