Individualize your apps

Unix Insider –

Are you ready for more pizzazz in your GUI applications? Most GUI toolkits provide several ways to configure such widget characteristics as color, font, and size. For the first installment of this three-column series, we look specifically at the Tk toolkit and its option database.

Tk is quite portable, so the examples here work under Mac OS, Unix, and Windows, among other operating systems. What's more, although these examples are coded in Tcl/Tk, each 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:

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.

Tk does indeed "have reasonable default values"; even beginners can build quite useful GUIs with Tk. After this column, though, you'll have a better grasp of how to advance beyond that level.

Using the Tk option database:

Read the whole series!

  • Part 1. Individualize your apps
  • Part 2. Making the most out of the option database
  • Part 3. Options for the Tk option database
  • Not a database to fear

    Don't be afraid of the option database. It's really just a small, flat configuration file. In other words, you can examine and update it as a simple text file, without the structural and transactional overhead of more formal databases. Here's a minimal example of an option database: a file in your Unix

    <font face="Courier">$HOME</font>
    directory called
    <font face="Courier">.Xdefaults</font>
    with contents:

    <font face="Courier">
         *background: blue
    </font>

    In English, this says, "Make the default background color for all my widgets in all my X applications, including Tk applications, blue." The format and interpretation of option databases are portable to Mac OS, OpenVMS, and Windows; the next installment will provide a few platform-specific tips to help start you off right if you're not using Unix.

    More generally, an option database is a file that lists key-value pairs. With it, you can customize the appearance of an application without making any changes to the code for that application. A common example is a specification that cranks up the font size for widgets. Suppose you create a Tk-based application and deliver copies to hundreds of users. Everything's going great, until a couple of users complain that the text is 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 those users who want larger text to include the following line in their resource databases:

    <font face="Courier">
         *font: {Courier 17}
    </font>

    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, which is familiar to many Unix users. The key is a multipart string to name the application, a widget to receive the new value, and an attribute. A colon and whitespace separate the key and value. Thus, a single line that says, "Make the background color of the buttons of all Tk applications cyan" appears in the options database as:

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

    Widget names can be wildcarded with

    <font face="Courier">*</font>
    , so it's legal to write:

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

    It's also possible to name specific widgets, so the option database might include the lines:

    <font face="Courier">
         Tk.label1.background: white
         Tk.label5.background: blue
    </font>

    As with widgets, the application name appears in one of three forms: the

    <font face="Courier">*</font>
    wildcard; a specific application name; or a capitalized class name. The
    <font face="Courier">Tk</font>
    immediately above is a class name. Any application built under Unix with Tk has the
    <font face="Courier">Tk</font>
    class name.

    An application name, on the other hand, specifies an executable instance. For example, when Tcl/Tk-based

    <font face="Courier">wish</font>
    is run as a console application from the desktop, its application name might be
    <font face="Courier">wish83</font>
    (version 8.3 of
    <font face="Courier">wish</font>
    ). But when coauthor Flick launches his test-engine tool, it comes up with an application name of
    <font face="Courier">tapioca</font>
    .

    Database management

    To this point, we've presented the option database in a Unix context as

    <font face="Courier">$HOME/.Xdefaults</font>
    . 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:

    <font face="Courier">
         option readfile $my_option_database
    </font>

    In this usage,

    <font face="Courier">$my_option_database</font>
    names a file in exactly the format we've already described, but in an arbitrary position in the filesystem. This might be a systemwide configuration, as in:

    <font face="Courier">
         option readfile /usr/local/options/monitor.db
    </font>

    or one customized for a particular user, such as:

    <font face="Courier">
         set my_option_database /home/aflick/options/.games.db
         option readfile $my_option_database
    </font>

    There are two additional ways in which a script can control widget appearance with even finer programmatic control. The Tk

    <font face="Courier">option</font>
    command also recognizes individual key-value elements with the
    <font face="Courier">add</font>
    subcommand. This gives the possibility of sequences, such as:

    <font face="Courier">
         option add *Text.background green
         option add *foreground black
    </font>

    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, such as:

    <font face="Courier">
         text .mytext -font {-family garamond size 24}
    </font>

    They can also be reconfigured at any time during Tk's execution:

    <font face="Courier">
         .mytext configure -font $a_better_font
    </font>

    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 it has apparently acquired. In our next column, we'll show you how to apply these principles to Windows and Mac OS. We will also discuss a few architectural concerns involved in distributing finished applications. Until then, bring color into your life: use the Tk option database!

    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 nonwidget 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, Mark Harrison and Michael McLennan (Addison Wesley, 1998): 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
  • Past Regular Expressions columns: http://www.itworld.com/Comp/2378/UIRRegularExpressions/
  • This story, "Individualize your apps" was originally published by ITworld.

    Copyright © 2001 IDG Communications, Inc.

      
    Shop Tech Products at Amazon