From: www.itworld.com
May 3, 2001 —
In
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 canvas and text 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.
Back to (optional) work
Recall from the first two installments of this series that Tk uses the option command for all actions taken against the option database. We've already looked at the option add and option readfile subcommands. In this column, we'll explain option clear and option get.
option clear resets all assignments made to the option database by previous option add or option readfile commands. The idea is to return to a fresh wish 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 ~/.Xdefaults. This can surprise newcomers. An example helps: if .Xdefaults in your home directory contains the line:
*Button.background: cyan
and you run the script:
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]
through a Tk processor, you'll see a display like this one:
Button5 is cyan (a particular shade of light blue) because the first option clear in the sequence above reverts the background to the one that .Xdefaults specifies.
A drawing package might use option clear as part of a New operation to wipe out any previous work.
Complement add with get
option get 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:
option get .b2 background ""
reports cyan, the background color the option database recommends for .b2. This might not be the actual color of .b2 at any particular time. The best way to retrieve the current actual color would be:
.b2 cget -background
In the example above, .b2 cget -background 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 option add arbitrary data, and later retrieve them with option get. This opens yet another approach to the code-data duality: a data structure can be serialized as a collection of option add commands, and later restored to working memory with proper invocation of option get.
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:
$sun = 3;
$sum += 1;
print "Sum is $sum.\n";
A overly hasty reader might expect Sum is 4 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 borderwidth. However, the corresponding database option resource is borderWidth -- and the associated option database class is BorderWidth!
In the final installment of this series, we'll put together an example of the sophistication possible with the option database.
Resources
Unix Insider