Tcl/Tk for Programmers: With Solved Exercises that Work with Unix and Windows / Edition 1

Tcl/Tk for Programmers: With Solved Exercises that Work with Unix and Windows / Edition 1

by J. Adrian Zimmer
ISBN-10:
0818685158
ISBN-13:
9780818685156
Pub. Date:
09/10/1998
Publisher:
Wiley
ISBN-10:
0818685158
ISBN-13:
9780818685156
Pub. Date:
09/10/1998
Publisher:
Wiley
Tcl/Tk for Programmers: With Solved Exercises that Work with Unix and Windows / Edition 1

Tcl/Tk for Programmers: With Solved Exercises that Work with Unix and Windows / Edition 1

by J. Adrian Zimmer

Paperback

$120.95
Current price is , Original price is $120.95. You
$120.95 
  • SHIP THIS ITEM
    Qualifies for Free Shipping
  • PICK UP IN STORE
    Check Availability at Nearby Stores

Overview

This introduction to Tcl/Tk bridges the gaps between introductions, comprehensive manuals, and collections of scripts that solve particular problems. There are over 200 exercises with solutions for both Unix and Windows platforms.

Tcl/Tk for Programmers introduces high-level Tcl/Tk scripting language to experienced programmers with either Unix or Windows backgrounds. It includes a short introduction to TCP/IP, introductions on writing client-side scripts and GUI interfaces as well as integrating scripts with C/C++. In addition to covering version 8.0/8.0, the book describes the major differences between version 8.0/8.0, 7.6/4.2, and the experimental alpha version 8.1/8.1. Zimmer has extensive knowledge of Tcl/Tk programming and currently runs a consulting and training company based on his experience.

Product Details

ISBN-13: 9780818685156
Publisher: Wiley
Publication date: 09/10/1998
Series: Practitioners , #33
Pages: 560
Product dimensions: 7.42(w) x 9.45(h) x 1.16(d)

About the Author

J. Adrian Zimmer was raised in Nebraska and lived in Canada, Denmark, and Iran as well as South Carolina, Oklahoma, North Dakota, and Massachusetts. Educated through a postdoc in mathematics, his publications include two books Tcl/Tk for Programmers, I.E.E.E. Computer Society Press, 1998 and  Abstraction for Programmers, McGraw Hill, 1985.

Read an Excerpt

Tcl/Tk for Programmers

With Solved Exercises that Work with Unix and Windows
By J. Adrian Zimmer

John Wiley & Sons

ISBN: 0-8186-8515-8


Chapter One

Getting Started

This chapter discusses some issues related to obtaining and running Tcl/Tk and introduces a couple of very simple example scripts.

1.1 Obtaining Tcl/Tk

Tcl/Tk was created by John Ousterhout, then a professor at the University of California at Berkeley. Later Dr. Ousterhout headed a team at Sun Microsystems that enhanced the language through version 8.0 for a few years. Recently, Dr. Ousterhout has started his own firm, Scriptics, which is building development tools. Tcl/Tk itself will remain freely available for use on multiple platforms. The development tools will be sold.

To obtain Tcl/Tk, check out the Scriptics Web site. You should be able to find source versions that will compile on several platforms and compiled versions for the Macintosh and for Windows platforms. The compilation process is not difficult.

There is a user group on the Internet named "comp.lang.tcl." If you have difficulty installing Tcl/Tk, you may find help there. I did. One advantage of an open system is that the accessibility of the source code helps to train a larger number of knowledgeable people than you are likely to find when the source code is unavailable.

Some other Web sites with information about Tcl/Tk are maintained by Hobbs, Laird, SCO, The Tcl/Tk Consortium, Virden, and Zimmer.

1.2 Interactive Execution

Tel, and its extension Tk, are interpreted. This means that to the programmer it appears as if the computer speaks Tcl/Tk. Programmers can enter commands and expect them to be carried out without any apparent need for translation. By interactive execution, I mean running Tcl/Tk scripts in just this way. The software that interacts with the programmer is called an interpreter. The same software runs prewritten Tcl/Tk scripts directly as described in the next section.

Tcl

The Tcl interpreter is often named tclsh?? where the question marks represent a version number. This version number may not be present.

Running under Windows 95, you can start the tclsh?? interpreter by just clicking on tclsh?? which can be found from the Start button. The effect is to bring up what looks like a DOS window with a % sign as the prompt.

Running under Unix, you will have to find out what the name for tclsh?? actually is and, possibly, what directory contains it. If you installed Tcl yourself, you will know this. Otherwise, ask your system administrator. Once you have the file name of the interpreter, you can execute it. The effect is the same as with Windows 95. The effect is to bring up a DOS window that is actually running the Tcl interpreter. The default prompt is again the % sign.

Here is a sample session as it would appear under either Windows 95 or Unix. Characters appearing after a % sign have been typed by a Tcl user. The rest has been printed by the Tcl interpreter:

% are you there? invalid command name "are" % puts "Hellow Orld" Hellow Orld % proc say_hello {} { puts "Hellow Orld" } % say_hello Hellow Orld % proc say_hello {} { return "Hellow Orld" } % say_hello Hellow Orld

From the tclsh?? interpreter, you can execute any command that Tcl knows simply by typing it as shown above. Everything you type after the % prompt is a command line. After a command line has been completed with the return/enter key, that command line is executed. The interpreter prints whatever the puts command sends to standard output. It also prints error messages and anything returned from a procedure. When done processing a command line, it prints a new % prompt.

The window in which the above activity takes place can be called the command window. When you invoke tclsh?? from X Windows, it is the same window in which you entered the tclsh?? command. When you invoke tclsh?? from Windows, it is a new DOS window. ("DOS" here refers to the kind of window and is not a claim that DOS is controlling what you see in the window-Tcl is.)

The proc command shown in the example is used to define a new procedure named say_hello. After it has been defined, this procedure can be executed like any Tcl command. Because Tcl is interpreted, procedures can be defined and redefined at will.

In fact, in this example, the procedure, say_hello , is implemented and executed twice. The first execution generates an empty return string which is not shown by the interpreter. What is shown is the output from a p u t s command. The second execution returns the same string. Because the return string is nonempty, the interpreter shows it.

While using the tclsh?? interpreter interactively, it is possible to execute Tcl statements that have been entered into a file. Tcl's source command serves this purpose. This command takes one argument, a file name. The effect of this command is to execute the statements in the named file. For example, suppose a file named hello contains these lines:

proc say_hello {} { puts "Hellow Orld" }

If hello is available in the current directory, then the following session is possible with tclsh??.

% source hello % say_hello Hellow Orld

You can enter a complete pathname instead of hello. This may not be necessary in Unix if you switch directories before starting the interpreter. In Windows 95, you will have to switch directories after starting the interpreter. Use Tcl's built-in cd command, for example,

% cd "C:\Program Files\Tcl Scripts"

The quotes are required because of the spaces in the pathname. When writing Windows pathnames for Tcl, you should replace every backslash "\" with a slash "/." Tcl will do the right thing when it passes the pathname through to Windows.

You can terminate the Tcl interpreter in several ways. One way is to use the exit command. When you follow this command with an integer on the command line, that integer will be the return code given to your operating system.

Remark

Default Unix installations of tclsh?? are set up so that when the interpreter runs interactively, it passes all unrecognizable commands to a Unix shell and then returns the results from that shell to the user. This means you can use Tcl as a kind of Unix shell. Until version 8.0 of Tcl, there was no similar mechanism in Windows installations.

Although useful when Tcl is used interactively, the mechanism is not useful with direct execution of Tcl scripts because it is not portable.

Tk

The Tk interpreter is usually named wish??. It can be run interactively the same way tclsh?? is. Because Tk is an extension of Tel, the points made about tclsh?? above apply.

With Tk, however, you see an extra window. This is called the root window and is for use by end users. The root window and similar windows created by your script are where the action will be.

Tk's command window is not useful in production scripts. Its sole value is that it lets you experiment with Tk interactively. It is true that Tk's command window could be used like Tcl's command window to communicate with the user through Unix/DOS-like input and output, but the point of Tk is to get away from such an interface.

Figure 1.2a shows an example of an interactive use of Tk through its command window. The root window appears at the top of the figure and the command window at the bottom. The root window is named "wish." Since the appearance of a command window varies greatly between Unix and DOS, only the text from that window is shown. This text shows that two commands were executed. The first created a label with the text "Hellow Orld" and the second posted that label to the root window.

A slightly more complicated example is shown in Figure 1.2b. The example shows two additional commands. The first of these creates a button and the second posts that button to the root window. The user can "push" the button by clicking on it with the mouse. The button is set up so that when the user does this the root window vanishes and the interpreter stops executing.

1.3 Direct Execution

By direct execution, I mean execution of a script file as if it were any other program known to your operating system. Your script is still interpreted but you do not fire the interpreter up first and interact with it. Instead, you start the script by telling the operating system to execute it. The operating system then figures out that execution requires that an interpreter be fired up to do the actual work.

With direct execution, there is no command window. Standard input and output will be through the window where the script is executed, if any. Under Windows 95, Tcl will provide a new DOS window for this purpose, if necessary.

Unix

From the first, Unix has had an elegant mechanism for executing scripts directly. The mechanism requires two preparatory actions:

1. The file containing the script must be declared executable with Unix's chmod command. On the Unix command line, enter

chmod +x script

where script names the file with your script.

2. The file containing the script must begin with the two characters #!. On the same first line as these characters, the pathname of the appropriate interpreter is listed. This will be the tclsh?? interpreter for Tcl or the wish?? interpreter for Tk.

When you have taken these two preliminary steps for a file script you can execute script with a normal Unix command line, for example,

script ARG1 ARG2 ... ARGn

where ARG1, ARG2, ... ARGn represent arguments passed to the script.

Windows 95

Scripts can be executed directly in the way Windows 95 expects programs to execute-by clicking on them. To arrange for this you need to give your script files a distinctive extension, such as tcl or tk , and to tell Windows about your extension.

To tell Windows about your extension, do the following:

1. From the Explorer click on "View," then "Options," then "File Types," and finally "New Type."

2. Now you have to enter a description such as "Tcl Script" and an extension such as "tel."

3. At the bottom of this window is an "Action" subwindow. Under that is a "New" button. Click on it.

4. Now you have another window that wants "Action" and "Application" infomation. Enter "open" as the action because "open" is the action Windows will use when you double-click on a file of the designated extension. You also need to enter an application for Windows to execute when it does the "open" action. For Tcl on my computer, I entered

c:\progra~1\tcl\bin\tclsh80.exe "%1"

5. Now back out of the data entry windows to the Explorer and double-click a file with the designated extension as a test.

If the file type already exists, you can look for it with the scrollbar window that you see after clicking "File Types." Then click on "Edit" and change the windows I have described above as necessary.

When Tcl or Tk is executed this way, it is called "direct execution." Tcl brings up a DOS window that is used for standard input and output-this is not a command window. Tk brings up a root window but not a command window. With either Tcl or Tk, commands are taken from the script file you double-click on. If that script begins with the first line shown above for Unix usage, it is OK. Tcl/Tk sees any line beginning with a # as a comment and ignores it.

Command Line Execution in Windows 95

If you execute Tcl as just described, you may find the DOS window disappears much too soon. This problem can be fixed with an extra input statement at the end of your scripts-if your script catches all possible errors.

Another fix is to execute your scripts from a batch file, tcl.bat, whose contents are something like:

echo off c:\progra~1\tcl\bin\tclsh80.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

The advantage of the batch file is that it creates the DOS window from which your script is executed-you will have good control over how long that window lasts. A disadvantage is that your scripts are limited to accepting eight or fewer arguments. That probably will not fret you.

If you drag tcl.bat from the Explorer to your desktop, Windows will create a shortcut to tcl.bat. After the shortcut is created, right click on the shortcut symbol, then on "Properties," and finally on the "Program" tab. Edit the "Cmd Line" field by adding a space and then a question mark to the end of the line. Enter the directory where you keep your Tcl scripts into the "Working" field. At the bottom of the dialog box, make sure there is no check mark in the "Close on exit" square. Exit the Properties window.

Now when you click on the shortcut you will get a window into which you can enter a command line for executing a Tcl script. This window pops up because you used a question mark as an argument in the "Cmd Line" field.

Remark

The arguments you enter into this window can be a bit confusing. In one sense, they are DOS arguments-it is DOS that will determine exactly what each argument is. This means you enter "Hellow Orld" and not {Hellow Orld}. In another sense, the contents of these arguments will be interpreted by Tel, not DOS. This means you enter C:/Dat/Whatever rather than C:\Dat\What ever.

1.4 Reading this Book

For a solid working knowledge of Tel, study Chapters 1-8. After that continue with Chapter 9 or skip to Chapter 11. When you have finished Chapter 19, you will have a solid working knowledge of Tk. You can add on the other topics as you need them.

In many chapters, there are sections which add no new facts about Tcl/Tk. These sections are useful because they provide examples that help you to develop a style for programming in Tcl/Tk that fits the language. However, they can be skipped on first reading. You can recognize these sections because their names are italicized.

The book is written for looking things up but do not expect all things to be present. Use your on-line manual if you do not see what you need in the book. Look for additional commands and look for additional ways of using commands that are in the book. There are two reasons you must do this: First, I have omitted some things to prevent the book from becoming deadly dull. Second, Tcl/Tk is constantly changing.

Here and there in the book, are script boxes which contain example Tcl or Tk scripts. For your convenience, the scripts in these boxes are also available in downloadable form. You can find them from this book's home page. The names of the downloadable files are the same as the names of the corresponding boxed scripts. The names, for example, "S4.5a" and "S 10.7b," are chosen to identify the sections in which the script boxes are found-the final letter of a name differentiates between multiple script boxes in the same section.

The same home page indexes scripts which solve exercises. Although all exercises are solved in this book, only some solutions are also available at the Web site. Example names for on-line exercise scripts are "ES5.8c" and "ES24.4bW." Strip these names of the capital letters and you have the exercise that the script solves. The additional "W" at the end of "ES24.4bW" indicates that this "script" is a C program that has been prepared for compilation on a Windows system.

All exercise numbers and script numbers end with lowercase letters. Remove this lowercase letter and you have the section where the exercise or script may be found.

Index entries are to just one place in the text. When other places in the text provide important additional information, there are cross references.

Conventions for Procedures

Here is a pattern that shows how a Tcl procedure is invoked:

PROC_NAME ARG1 ARG2 ... ARGn

Each of ARG1, ARG2, and so on, must match up with a variable found in the definition of the procedure, for example,

proc PROC_NAME {VAR1 VAR2 ... VARn} { ... }

This kind of matching is done in almost all programming languages, but the terms to describe it are not universal. In this book each of ARG1, ARG2, and so on, is called an argument and each of the corresponding variables, VAR1, VAR2, and so on, is called a parameter.

Tcl procedures and commands always return a value. When that value is always an empty string, the description in this book is not likely to mention it.

(Continues...)



Excerpted from Tcl/Tk for Programmers by J. Adrian Zimmer Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Table of Contents

Forward.

Acknowledgements.

1. Getting Started.

I: Tcl.

2. Basic Syntax and I/O.

3. Expressions and Branching.

4. Procedures.

5. Data Structures and Iteration.

6. Strings, Files, and Glob Pattern Matching.

7. Regexp and Regsub.

8. Tcl Odds and Ends.

9. More about Procedures.

10. TCP/IP Networks and Event-Driven Programming.

II: Tk.

11. Overview of Tk.

12. Widget Characteristics.

13. Geometry Management.

14. Some Basic Widget Types.

15. List of Widget Types.

16. Bindings.17. Partially Displayed Widgets.

18. Text Widgets.

19. Canvases.

20. Tk Odds and Ends.

21. The Browser Plugin and Safe-Tcl.

III: C/C++.

22. The C/C++ Connection.

23. Essential Library Functions.

24. Some Useful Library Functions.

25. Creating Tcl Objects in C/C++.

Bibliography.

Index.
From the B&N Reads Blog

Customer Reviews