www.gibmonks.com

  Previous section   Next section

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

Table of Contents
Chapter 37.  Tk Widget Attributes


Configuring Attributes

You specify attributes for Tk widgets when you create them. You can also change them dynamically at any time after that. In both cases the syntax uses pairs of arguments. The first item in the pair identifies the attribute, the second provides the value. For example, a button can be created like this:

button .doit -text Doit -command DoSomething

The name of the button is .doit, and two attributes are specified: the text and the command. You can change the .doit button later with the configure widget operation:

.doit configure -text Stop -command StopIt

The current configuration of a widget can be queried with another form of the configure operation. If you just supply an attribute, the settings associated with that attribute are returned:

.doit configure -text
=> -text text Text {} Stop

This command returns several pieces of information: the command line switch, the resource name, the resource class, the default value, and the current value. If you don't give any options to configure, then the configuration information for all the attributes is returned. The following loop formats the information:

foreach item [$w configure] {
    puts "[lindex $item 0] [lindex $item 4]"
}

If you just want the current value, use the cget operation:

.doit cget -text
=> Stop

You can also configure widget attributes indirectly by using the resource database. An advantage of using the resource database is that users can reconfigure your application without touching the code. Otherwise, if you specify attribute values explicitly in the code, they cannot be overridden by resource settings. This is especially important for attributes like fonts and colors.

The tables in this chapter list the attributes by their resource name, which may have a capital letter at an internal word boundary (e.g., activeBackground). When you specify attributes in a Tcl command, use all lowercase instead, plus a leading dash. Compare:

option add *Button.activeBackground red
$button configure -activebackground red

The first command defines a resource that affects all buttons created after that point, and the second command changes an existing button. Command-line settings override resource database specifications. Chapter 28 describes the use of resources in detail.


      Previous section   Next section
    Top