Composite fields megawidgets

See also What is a megawidget?

Richard Suchenwirth 2001-10-23 - Here is a code snippet for specifying composite fields: a number of different widgets (label, entry) arranged horizontally in a frame, which you might call a "mini-megawidget" (it has no special commands other than those of a frame, but the textvariables do the job...). It simplifies the production of self-explaining GUIs, though the layout, especially field widths, is not optimized yet. Anyway - play with it, make it better! A "fields" is specified like this:

    fields .1 {"Filter lines with" $FILTER '$NFILTERED}
    fields .2 {"Field separator" $FS}
    fields .3 {"Non-empty field" $NEF1 '$N1 = '$NPERCENT1 %}
    pack .1 .2 .3 -side top

The first argument is the widget name, the second contains the "fields" as a list. If a "field" begins with "$", it is rendered as a entry widget bound to the global variable of same name, which thusly can be modified from the GUI. If it begins with "tick-dollar('$)", a label with textvariable is created instead, so this is for read-only variables. All other texts are taken verbatim onto a label widget.

Note that the dollar sign has a different semantics here (hence, the fields list must be braced to prevent the Tcl parser from doing substitutions): it indicates a global variable of that name, but is not to be substituted by the parser. The "tick-dollar" style makes it evident that this isn't just normal Tcl... ;-)

 proc fields {w contents} {
    frame $w
    set n 0
    foreach i $contents {
        if [string match $* $i] {
            entry $w.$n -textvariable [string range $i 1 end]
        } elseif [string match '$* $i] {
            label $w.$n -textvariable [string range $i 2 end]
        } else {
            label $w.$n -text $i

        incr n
    eval pack [winfo children $w] -side left -anchor w
    set w