Buttons Associated with Tcl Variables

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 27.  Buttons and Menus


The checkbutton and radiobutton widgets are associated with a global Tcl variable. When one of these buttons is clicked, a value is assigned to the Tcl variable. In addition, if the variable is assigned a value elsewhere in the program, the appearance of the checkbutton or radiobutton is updated to reflect the new value. A set of radiobuttons all share the same global variable. The set represents a choice among mutually exclusive options. In contrast, each checkbutton has its own global variable.

The ShowChoices example uses a set of radiobuttons to display a set of mutually exclusive choices in a user interface. The ShowBooleans example uses checkbutton widgets:

Example 27-4 Radiobuttons and checkbuttons.

graphics/27fig03.gif

 proc ShowChoices { parent varname args } {    set f [frame $parent.choices -borderwidth 5]    set b 0    foreach item $args {       radiobutton $f.$b -variable $varname \          -text $item -value $item       pack $f.$b -side left       incr b    }    pack $f -side top } proc ShowBooleans { parent args } {    set f [frame $parent.booleans -borderwidth 5]    set b 0    foreach item $args {       checkbutton $f.$b -text $item -variable $item       pack $f.$b -side left       incr b    }    pack $f -side top } set choice kiwi ShowChoices {}choice apple orange peach kiwi strawberry set Bold 1 ; set Italic 1 ShowBooleans {}Bold Italic Underline 

The ShowChoices procedure takes as arguments the parent frame, the name of a variable, and a set of possible values for that variable. If the parent frame is null, {}, then the interface is packed into the main window. ShowChoices creates a radiobutton for each value, and it puts the value into the text of the button. It also has to specify the value to assign to the variable when the button is clicked because the default value associated with a radiobutton is the empty string.

The ShowBooleans procedure is similar to ShowChoices. It takes a set of variable names as arguments, and it creates a checkbutton for each variable. The default values for the variable associated with a checkbutton are zero and one, which is fine for this example. If you need particular values, you can specify them with the -onvalue and -offvalue options.

Radiobuttons and checkbuttons can have commands associated with them, just like ordinary buttons. The command is invoked after the associated Tcl variable has been updated. Remember that the Tcl variable associated with the button is defined in the global scope. For example, you could log the changes to variables as shown in the next example.

Example 27-5 A command on a radiobutton or checkbutton.
 proc PrintByName { varname } {    upvar #0 $varname var    puts stdout "$varname = $var" } checkbutton $f.$b -text $item -variable $item \    -command [list PrintByName $item] radiobutton $f.$b -variable $varname \    -text $item -value $item \    -command [list PrintByName $varname] 

       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net