Tcl/Tk Cookbook - Basics of Tk


Purpose

A simple example introduces the basics of widgets based programming using the Tk toolkit. The same application is used to demonstrate the Tk send command.

You will need

At this point it is assumed that the reader has sufficient familiarity with Tcl syntax and built-in Tcl commands. If you are new to the terminology of window-based application development or wish to know more about graphical user interface design, reading the introductory chapters of Visual Design with OSF/Motif by Shiz Kobara [3] is a good source of reference. However, Tk makes it very easy to learn widget programming and this application is simple enough to grasp the basics. Quick Tour provides the briefing you need.

Dish to Serve Up:

An application to browse and modify a button widget's attributes (configuration options). In part-II split the application into two parts and communicate via send

Recipe

Since this application has more than a few lines of script, we recommend that you create, execute and edit the script from a file called appl1.tcl, preferably under a directory ~/examples/chapter1. Create the directory ~/examples/chapter1, cd to it and use the editor of your choice to create the file appl1.tcl. Remember to make this file executable by typing chmod u+x appl1.tcl before you execute the script.

Part-I

  1. create a frame to contain the button and the set of attribute - value pairs.
  2. create the button and place it in the parent and get current configuration options (list of attribute - value pairs).
  3. display each pair within a frame of their own, the attribute name in a label and the associated value in an entry widget.
  4. bind event in entry widgets to an action to modify an attribute to the new value entered.

Part-II

We use the above application to demonstrate the Tk command send. We achieve this by splitting appl1 into two applications. The first one creates just the button with the text string and the command to exit. It also contains the procedure reJig with its input parameters altered. The second application contains the resource form in which the user edits the values. send is used to communicate between the application.

For this part,you need two separate Tcl/Tk script files. Place appl1 script in file av and the script for av2 under ~examples/chapter1

  1. create appl 1 - a frame with a button.
  2. create appl 2 base - a frame to hold the set of attribute-value pairs
  3. procedure to display attribute - value pairs of the appl 1 widget in appl 2.
  4. send a request to app1 from app2 to send the list of default settings.
  5. bind event in entry widgets in appl 2 to an action to send the changed value and the attribute name to appl 1 and invoke an action in appl 1 to reconfigure the attribute.