Options for the Tk option database

In the first installment of this series, we mentioned three books as references for the use of the option database in its specific bindings to the Perl, Python, and Tcl languages (see the Resources section at the end of this article for pointers to this and other information). It was an oversight not to recommend Mark Harrison and Michael McLennan's Effective Tcl/Tk Programming at the same time. As master programmer and Expect inventor Don Libes wrote, "Their book is a gem and is the one that finally taught me to use the option database. They use options throughout the entire book -- which makes sense, since their whole book is about 'effective programming.'" Start with section 8.1, "Adding Polish to Your Application."

Effective Tcl makes good reading

Effective Tcl/Tk Programming is good reading for any Tk user, including those who rely on PerlTk and Tkinter. Last month we recommended John Grayson's book on Tkinter to anyone scripting graphical user interfaces, whatever the toolkit. Although Grayson's examples are exclusively in Python, his design principles transcend any particular language. Similarly, Harrison and McLennan communicate coding ideas that you'll find useful no matter what language you're using. For example, their presentation of Tk correctly emphasizes the power and flexibility of the built-in

<font face="Courier">canvas</font>
and
<font face="Courier">text</font>
widgets.

Effective Tcl/Tk Programming also gives a clear exposition on the use of a script as a configuration file to be evaluated. Sections 4.7.8, "Saving a Drawing," and 7.6.6.3, "Using Tcl Commands to Express Data," explore the notion that "Tcl commands are a powerful way of expressing data." Our last column gave several other examples showing that this holds true for commands in any scripting language.

Another strength of Harrison and McLennan's book is its presentation in chapter 7 of the event-based programming style that we've covered in past Regular Expressions columns.

Using the Tk option database:

Read the whole series!

Back to (optional) work

Recall from the first two installments of this series that Tk uses the

<font face="Courier">option</font>
command for all actions taken against the option database. We've already looked at the
<font face="Courier">option add</font>
and
<font face="Courier">option readfile</font>
subcommands. In this column, we'll explain
<font face="Courier">option clear</font>
and
<font face="Courier">option get</font>
.

<font face="Courier">option clear</font>
resets all assignments made to the option database by previous
<font face="Courier">option add</font>
or
<font face="Courier">option readfile</font>
commands. The idea is to return to a fresh
<font face="Courier">wish</font>
process, one initialized only with the platform-specific resource database values. Tk implements this by dropping all values, and, at the time of the next use of the resource database, re-reading the external
<font face="Courier">~/.Xdefaults</font>
. This can surprise newcomers. An example helps: if
<font face="Courier">.Xdefaults</font>
in your home directory contains the line:

<font face="Courier">
     *Button.background: cyan
</font>

and you run the script:

<font face="Courier">
     pack [button .b1 -text Button1]
     pack [button .b2 -text Button2 -bg red]
     pack [button .b3 -text Button3]
     option add *Button.background blue
     option add *Button.foreground yellow
     pack [button .b4 -text Button4]
     option clear
     pack [button .b5 -text Button5]
     option clear
     pack [button .b6 -text Button6]
     pack [button .b7 -text Button7]
     option clear
     pack [button .b8 -text Button8]
</font>

through a Tk processor, you'll see a display like this one:

<font face="Courier">Button5</font>
is cyan (a particular shade of light blue) because the first
<font face="Courier">option clear</font>
in the sequence above reverts the background to the one that
<font face="Courier">.Xdefaults</font>
specifies.

A drawing package might use

<font face="Courier">option clear</font>
as part of a
<font face="Courier">New</font>
operation to wipe out any previous work.

Complement add with get

<font face="Courier">option get</font>
is more interesting. It is used to interrogate the option database to determine which widget default property assignments have already been made. Note the distinction:

<font face="Courier">
     option get .b2 background ""
</font>

reports cyan, the background color the option database recommends for

<font face="Courier">.b2</font>
. This might not be the actual color of
<font face="Courier">.b2</font>
at any particular time. The best way to retrieve the current actual color would be:

<font face="Courier">
    .b2 cget -background
</font>

In the example above,

<font face="Courier">.b2 cget -background</font>
returns red.

Most applications use the option database to manage properties of visual widgets. However, the coupling between the database and Tk widgets is a loose one. In fact, the database is general-purpose, without any necessary connection to Tk graphics. It's possible to

<font face="Courier">option add</font>
arbitrary data, and later retrieve them with
<font face="Courier">option get</font>
. This opens yet another approach to the code-data duality: a data structure can be serialized as a collection of
<font face="Courier">option add</font>
commands, and later restored to working memory with proper invocation of
<font face="Courier">option get</font>
.

Spelling matters

This freedom leads to a challenge characteristic of conventional scripting languages: spelling matters. Because the option database is willing to accept anything, the Tk processor doesn't complain about misspellings. It simply puts misspelled data in a place they won't be found, or reports them as null. The effect is a bit like the one typical with local variables. Take this example in a Perl program:

<font face="Courier">
     $sun = 3;
     $sum += 1;
     print "Sum is $sum.\n";
</font>

A overly hasty reader might expect

<font face="Courier">Sum is 4</font>
as output.

This is crucial for widget work, where capitalization is rather overloaded. Consider the width of a widget's border. This attribute is called

<font face="Courier">borderwidth</font>
. However, the corresponding database option resource is
<font face="Courier">borderWidth</font>
-- and the associated option database class is
<font face="Courier">BorderWidth</font>
!

In the final installment of this series, we'll put together an example of the sophistication possible with the option database.

Resources

This story, "Options for the Tk option database" was originally published by ITworld.

Copyright © 2001 IDG Communications, Inc.

  
Shop Tech Products at Amazon