Options for the Tk option database

By Cameron Laird, Unix Insider |  Development Add a new comment

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 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:

    Add a comment

    Post a comment using one of these accounts
    Or join now
    At least 6 characters

    Note: Comment will appear soon after you have activated your account.
    Obscene/spam comments will be removed and accounts suspended.
    The information you submit is subject to our Privacy Policy and Terms of Service.

    ITworld LIVE

    DevelopmentWhite Papers & Webcasts

    White Paper

    HP NonStop SQL Fundamentals whitepaper

    This whitepaper offers a detailed look into the fundamentals of HP NonStop SQL solutions. See how this system delivers unprecedented levels of application availability with fail-safe data integrity and meets the needs of enterprises with large-scale business critical applications.

    White Paper

    Nebraska Medical Center case study

    See how the Nebraska Medical Center implemented a SQL solution to make information more readily available to streamline operations, improve patient care and facilitate medical research with an enterprise solution running on HP NonStop servers.

    White Paper

    Concepts of NonStop SQL/MX

    For DBAs and developers who are familiar with Oracle solutions and want to learn about NonStop SQL/MX, this whitepaper provides an overview of the similarities and differences between the two products-with a specific focus on implementation.

    White Paper

    6 Things Your CIO Needs to Know About Requirements

    If your organization is not predictably successful on technology projects, there is likely an issue in requirements. CIOs must take action and own requirements maturity improvement. There are 6 main things a CIO must know about requirements.

    Webcast On Demand

    User Experience Monitoring

    In this webinar, you will learn hints & tips for improving end-user response times from Forrester Research analyst, Jean-Pierre Garbani.

    Sponsor: Nimsoft

    See more White Papers | Webcasts

    Ask a question

    Ask a Question