Tcl/Tk Cookbook - Basics of Tcl


Purpose
This simple Tk application enables the reader to learn Tcl syntax and built-in commands by browsing syntax descriptions and example code. The reader can execute the example to see the result, edit and test the example code to experiment variations - all from within this application. The reader can also create his own notes on specific topics presented as well.

You will need

At this point it is assumed that the reader has read through the chapter "Getting Started" and has access to Tcl and Tk and this application to try. To invoke "Help" in this application, you may need to set the absolute pathname of the "tcltk-man-html" directory and the browser you wish to invoke by editing two lines in the source code hyper.tcl.

Acknowledgement

This module makes use of the Latex document Tcl/Tk Reference Guide for Tcl 7.4/Tk4.0 written by Paul Raines and Jeff Tranter . It is also tailored to display the contributed Tcl/Tk HTML documents as an option.

Usage

Selecting a Topic

Invoke this application by typing hyper.tcl at command level from one of the windows in your display. If you get the message command not found check if the pathname to Tk wish has been set up properly. Housekeeping explains how to do this. If you do not have write permission to the source code, invoke wish interactively and type source hyper.tcl . A scrollable Tk window with a list of Tcl topics (Figure below - shown smaller than the actual size) will appear. Depending upon the mouse cursor position one of the list item will be highlighted. You can exit the application anytime by clicking on the "Quit" button.

Note on Selecting: You can select any of the list items by moving the mouse pointer onto that item and clicking the lift mouse button. Throught all the examples, pressing and releasing the left mouse button on a menu-button or a pushbutton or a hyper-link item selects it.

In text widgets, for actions such as copy & cut, press the left mouse button at the beginning of the section you want to select and holding the button down drag it along the section up to the end of the sectionyou want and then release the button.

Reference Window

Select the topic you want. A second window, titled "Quick Reference", like the one below, will appear.

This window displays a brief description of the commands or variables or features of the topic you chose. On the righthand side of the top menubar, a button for "manual Pages" will be displayed in normal state if you have access to tcltk-man-html and a HTML browser.

Selecting the "Dismiss" button closes this toplevel window but it is not necessary to close this window. When you select the next topic, the contents of the text widget is refreshed.

"Examples for...", at the top lefthand corner of the "Quick Reference" window is a pulldown menu whose items are the keywords of the chosen topic for which some simple examples are given. For example, if the chosen topic is "Control Flow" the menu items will be if, while, for etc.

Example Window

Selecting a keyword displays a very simple piece of example code in another toplevel window, titled "Example Browser" similar to :

As before selecting the "Dismiss" button closes this toplevel window too but it is not necessary to close these windows. When you select the next topic or keyword or example code, the contents of the text widget is refreshed.

The top menubar of this Example window has a pulldown menu titled "See Also ..."

which lists the names of all the example files, from the Examples directory, that contain the keyword chosen.

Testing

Clicking on the "Apply" button on the lower left corner of the "Example Browser" executes the example, displaying the results in a toplevel "Output" window.

The text window in "Example Browser", displaying the source code allows editing the displayed text. The reader is encouraged to edit the source code, execute and learn by experimenting how Tcl works.

As before selecting the "Dismiss" button closes this toplevel window too but it is not necessary to close these windows. When you select the next topic or keyword or example code, the contents of the text widget is refreshed.

Making Notes

Any change made to the example source is temporary but the users can make their own notes. Clicking on the "Notes" button at the bottom right-hand side displays an editable text window for making personalised notes on the chosen Tcl topic.

Selecting the "Save" option saves the contents of the text widget under the user's home directory and selecting "Print" sends the a hardcopy request of the saved file to the user's default printer.

Application Source Code

The source code for this Tcl application is under the directory ~code/ch1. If you are new to Tcl, we hope that by the end of this Cookbook, you would have gained enough familiarity with Tcl and Tk to understand the source code of this application.