FILE_TYPE: VIEWSCOLUMN KEYWORDS: scripting, GUI, Tk, options AUTHOR: Allen Flick and Cameron Laird HEAD: Individualize your apps DECK: Tk option database gives flexible control over GUI appearance SUMMARY: Introduction to management of Tk widget attributes (1100 words) END_SUMMARY Are you ready for more pizzazz in your GUI applications? The option database is a slick way to customize applications. Until now, too few programmers have been comfortable using it, though. SUBHEAD: How did you get along without it? First, a bit of background: most GUI toolkits provide several ways to configure such characteristics of individual widgets as their color, font, size, and so on. In this month's column, we'll look specifically at the Tk toolkit, and its option database. Tk is quite portable, so the examples here work under MacOS, Unix, and Windows, among other operating systems. What's more, although these examples are coded in Tcl/Tk, every one can be adapted immediately for the related PerlTk, Tkinter (Python), TkLua, and other Tk-derived bindings. Many successful Tk programmers are barely aware of the option database. Recent books by John Grayson, Nancy Walsh, and Chris Nelson have only a couple of pages each on this subject, although all three of these books capture the essentials accurately. This continues a tradition begun by Tk's author, John Ousterhout, who wrote in his 1994 book that
The option database shouldn't be needed very often in Tk applications because widgets have reasonable default values ... The option database exists primarily to provide cultural compatibility with other X toolkits; I suggest that you use it as little as possible.
One of Tk's strengths is that it does indeed "have reasonable default values"; even beginners make quite useful GUIs with Tk. After this column, though, you'll have a better grasp of how to advance beyond that level. SUBHEAD: Not a database to fear Don't be afraid of the option database. It's really just a small "flat" configuration file. That is, you can examine and update it as a simple text, without the structural and transactional overhead of more formal databases. Here's a minimal example of an option database: a file in your Unix $HOME directory called .Xdefaults with contents
     *background: blue
In English, this says: "make the default background color for all my widgets in all my X applications, including Tk applications, blue." While the format and interpretation of option databases are portable to MacOS, OpenVMS, and Win*, the next installment of "Regular Expressions" will provide a few platform-specific tips that will help start you off right if you're not using Unix. More generally, an option database is a file which lists key-value pairs. With it, you can customize the appearance of an application without making any changes to the code for the application. A common example--increasingly common as the computer-using population ages!--is a specification that cranks up the font size for widgets. Suppose you create a Tk-based application. It's successful: you deliver copies to hundreds of users. Everything's going great, until a couple of users complain that the text is just too tiny. The option database answers their need with no requirement to alter your application, pass it through quality assurance again, redeploy it in separate versions, or any related complications. Instead, just instruct your users to include in their resource databases the line
     *font: {Courier 17}
SUBHEAD: Syntax of the database Refinements of the basic key-value syntax give more precise control of screen appearance. The format is exactly that of X's resource database, familiar to many Unix users. The key is a multi-part string which names the application, a widget to receive the new value, and an attribute. Between the key and value are a colon and whitespace, so that a single line looks something like This says, "make the background color of the buttons of all
     Tk.Button.background: cyan
Tk applications cyan." Widget names can be wildcarded with '*', so it's legal to write
     Tk*background: cyan
It's also possible to name specific widgets, so the option database might include the lines
     Tk.label1.background: white
     Tk.label5.background: blue
As with widgets, the application name appears in one of three forms: * the '*' wildcard; * a specific application name; or * a capitalized "class name". The "Tk" immediately above is a class name. Any application built under Unix with Tk has the "Tk" class name. An application name, on the other hand, specifies an executable instance. Here are a couple of examples of application names: * When Tcl/Tk-based wish is run as a "console application" from the desktop, its application name might be wish83 (version 8.3 of wish). * When co-author Flick launches his test-engine tool, it comes up with an application name of tapioca. SUBHEAD: Database management To this point, we've presented the option database in a Unix context as $HOME/.Xdefaults. As with most things X, there is an abundance of variations on this theme. Developers have a great deal of flexibility in specifying options to their Tk applications. After a Tk application has begun, it can access an alternative option database under programmatic control with
     option readfile $my_option_database
In this usage, $my_option_database names a file in exactly the format we've already described, but in an arbitrary position in the filesystem. This might be either a system-wide configuration
     option readfile /usr/local/options/monitor.db
or one customized for a particular user
     set my_option_database /home/aflick/options/.games.db
     option readfile $my_option_database
For even finer programmatic control, a script can control widget appearance in two more ways. The Tk option command also recognizes individual key-value elements with the add subcommand. This gives the possibility of sequences such as
     option add *Text.background green
     option add *foreground black
The final alternative an options-database programmer needs to remember is the one generally taught first to Tk developers: programmatic control of the attributes of individual widgets. Attributes of a widget can be initialized at the time they're first created
     text .mytext -font {-family garamond size 24}
They can also be reconfigured at any time during Tk's execution:
     .mytext configure -font $a_better_font
SUBHEAD: Is your world all black and white? It's a bit surprising that more programmers don't exploit the option database. Our introduction aims to dispel the intimidating reputation that it has apparently acquired. In our next column, we'll show how to apply these principles to Windows and Macintosh, and discuss a few "architectural" concerns involved in distributing finished applications. Until then, bring color into your life: use the Tk option database! :END_BODY BIO: Guest columnist Allen Flick is Senior Test Engineer for InterVoice-Brite. He was the key developer of the "Tapioca" Tcl/Tk-based test engine used at DSC Communications, the company now known as Alcatel USA. Tapioca was presented to the San Diego Tcl/Tk Conference of September 1998. Cameron Laird is Vice President of Phaseit, Inc. :END_BIO RESOURCES: Options and Tk - A Beginner's Guide: http://www.cs.man.ac.uk/~fellowsd/tcl/option-tutorial.html Cameron Laird's personal notes on GUI toolkits http://starbase.neosoft.com/~claird/comp.windows.misc/toolkits.html Python and Tkinter Programming: http://www.manning.com/Grayson/ Tcl/Tk Programmer's Reference: http://www.wizvax.net/chrisn/TclTkProgRef/ Learning Perl/Tk: Graphical User Interfaces with Perl: http://www.oreilly.com/catalog/lperltk/index.html Tcl and the Tk Toolkit: http://www.awlonline.com/product/0,2627,020163337X,00.html Kenton Lee: Using X Non-Widget Resources: http://www.rahul.net/kenton/txa/aug95.html Cameron Laird's personal notes on the X Window System http://starbase.neosoft.com/~claird/comp.windows.x/X.html Practical Programming in Tcl and Tk: http://www.beedub.com/book/ Effective Tcl/Tk Programming: Writing Better Programs with Tcl and Tk: http://cseng.aw.com/bookdetail.qry?ISBN=0-201-63474-0&ptype=0 Using TCL/TK for an Automatic Test Engine: http://www.usenix.org/publications/library/proceedings/tcl98/full_papers/flick/flick_html/flick.html Full listing of past "Regular Expressions" columns: http://www.sunworld.com/sunworldonline/common/swol-backissues-columns.html#regex :END_RESOURCES