ITworld.com
  Search  
ITworld Home Page ITworld Webcasts ITworld White Papers ITworld Newsletters ITworld News ITworld Topics Careers ITworld Voices ITwhirled Changing the way you view IT

Options for the Tk option database

Unix Insider 7/21/00

Cameron Laird and Allen Flick, Unix Insider

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."

On this topic

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.

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

    UIR000721regex
    Display generated by above script

    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

  • "Individualize Your Apps," Cameron Laird and Allen Flick -- Part 1 in this series: http://www.itworld.com/AppDev/1243/UIR000602regex/
  • "Making the Most of the Option Database," Cameron Laird and Kathryn Soraiz -- Part 2 in this series: http://www.itworld.com/AppDev/1243/UIR000616regex/
  • Effective Tcl/Tk Programming: Writing Better Programs with Tcl and Tk, Mark Harrison and Michael McLennan (Addison Wesley, 1998): http://www.awlonline.com/product/0,2627,0201634740,00.html
  • Past Regular Expressions columns: http://www.itworld.com/Comp/2378/UIRRegularExpressions/
  • Cameron Laird manages his own software consultancy, Phaseit, from just outside Houston.

    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 in September 1998.




    Sponsored Links

    Great Deals On FUJITSU Notebooks @ Synnex!
    SYNNEX RESELLERS - Check Out The Savings On Lifebook Notebooks, Tablet PCs, And Ultra-Mobile PCs!
    Rebates On Motion Computing C5 Tablet PC!
    SYNNEX RESELLERS – This Mobile Clinical Assistant Is Perfect For Any Health Care Provider.
    RESOLVE SUPPORT ISSUES from your Desktop!
    Minimize downtime with a remote support solution that lets you resolve issues right from the desktop
    Used and Refurbished Cisco Routers
    Purchase Your Routers From Network Liquidators. Savings of Up to 90% with a Lifetime Warranty!
    Used and Refurbished HP ProCurve Switches
    Lifetime Warranties, Professional Testing & Shipping on all HP Equipment Purchases!
    » Buy a link now

    Advertisements
    Sponsored links
    Top 5 Reasons to Combine App Performance and Security
    Bring harmony to your mix of UNIX-Linux-Windows computing environments
    Locate Hidden Software on business PCs with this free tool
    KODAK i1400 Series Scanners stand up to the challenge
     Home   Application Development  Programming tools  Programming languages  Server Scripting
    www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
    storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

     
    Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

    CIO   Computerworld   CSO   GamePro   Games.net   Industry Standard   Infoworld   ITworld  
    JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

    DEMO   IDG Connect   IDG Knowledge Hub   IDG TechNetwork   IDG World Expo  

    Copyright © Computerworld, Inc. All rights reserved

    Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.