Tcl/Tk is the user-friendly way for anyone to produce GUI "windows" applications. A Tcl script does not need to be compiled to be used yet provides much more power than a "bat" file or shell script. Tcl is an open source language designed not as a replacement for Java or C++ but as a scripting language to "glue" software components written in those languages together in a flexible reusable framework. Its' simple rules allow anyone to create a functional GUI which will work in any variant of Microsoft Windows, Linux or even within a web browser. The Tool Command Language, Tcl, and its associated graphical toolkit, Tk are used to implement the examples in this guide. No formal training in Software Engineering or Computer Science is required nor is an advanced computer background assumed. To complete this exercise you will only need access to an MS Windows 95, 98, ME, NT or Linux computer system. After completion of the exercises, the reader will be able to produce medium complexity cross-platform windowed applications and will have an appreciation of the power and flexibility of Tcl/Tk. Links to other free resources regarding Tcl/Tk are provided at the end of this document. Typical time to complete this exercise: 2 hours. |
Step 1: Installation of Tcl/Tk.
Linux Mandrake and Redhat users should already have it in their base install. Verify this with the command "rpm -q tcl tk". If not, install these packages from your distribution CD or download from rpmfind.net. MS Windows users will need to download and install the Cygwin DLL package from www.cygwin.com. Click on the icon in the top-right of this web page to download and run the setup program. The current version of Cygwin is 1.3.2, released on 21 May 2001. Choose the installation defaults. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Step 2: Starting the Windowing Shell, wish
Skip to step 3 once you have started the windowing shell, wish. Linux users: first start Xwindows with Gnome, KDE, or your choice of desktop, and then execute "wish80 &". You may type this from an xterm or konsole window. In KDE, you may also press alt-F2, to start a program by name. Windows users: first start Cygwin's bash by double clicking the Cygwin desktop icon which was installed in step 1. To run the windowing shell, type "cygwish80 &" at the bash prompt. You may also use the Start...Run command and browse to C:\cygwin\bin\cygwish80.exe and run it directly without first starting Cygwin. As an aside, Cygwin also provides a completely open (and free!) implementation of Xwindows XFree86 for MS Windows. I've tried it and it works great. Cygwin even comes with OpenSSH 2.9p2 which supports protocols 1.5 and 2.0. [translation: I've deinstalled Hummingbird Exceed, puTTY and Reflection X from my Win98 box]! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Step 3: Practicing WISH
At this point you should see (at least) two windows on your desktop, a Tcl console and one labelled WISH. If not, please return to step 2. The remainder of the discussion will not distinguish between Windows and Linux environments, but we need to learn how to restart the windowing shell, WISH, on our chosen platform. Close the wish window. Notice how the console window disappears also. Do you remember how to restart wish? See step 2 for details. Windows users note that you cannot start cygwish80 from the Run menu without giving the entire path, but you can execute "cygwish80 &" from the bash prompt. FYI -- Cygwin gives you a bash prompt [and vi!] from within MS Windows. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Step 4: Hello, World!
Let's make a simple window which will have a button labeled "Hello, World!" that terminates and disappears when you push the button. Cut the following two lines and paste them into your Tcl Console window. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Step 5: Source
Notice the 1st choice under the File menu of the Tcl Console program. "Source" is the Tcl word to read in a script file. In a sense it is like the Load menu choice in many programs, but does not remove the previous file which you will experience should you ever source two files (or the same file twice). Cut the following 7 lines and create a file called hello.tcl. Use your multitasking prowess to launch notepad or vi without closing wish. Note that the & lets you run commands simultaneously from the bash prompt. If you didn't use &, the program started by bash must finish before you would be prompted to start another one. to indicate the name of the script language processor. This allows others to see just the wish window. We will use the Tcl Console to develop and test our scripts, but you will be pleased to know that the final product will not need a console window. Only the graphical window will be seen. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Step 6: Font Control
Create a file called fontopt.tcl containing the following lines: Each of the lines beginning with the word font must appear on a single line in the file. If these are broken by cutting and pasting operation you must mark each line break with a backslash continuation character. You may add multiple line continuation characters to suit your style/taste. Source the resulting file into wish. Notice this time that the buttons don't do anything. To exit this program you will need to use the X on the window. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Step 7: Create a file called b1.tcl containing the following lines.
Source it into wish. Push the button. Notice that the program does not exit. The puts (put string) allows one to trace the operation of the script using the console window while developing the application. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Step 8: Create a file called b2.tcl containing the following lines.
Source the file into wish. Push the lower button. Push it again several times. Push the top button. Push the bottom button again. This program is responding to your whims. Notice that count is a variable while .count is a widget. Notice the mandatory use of the line continuation character in the definition of .count. Notice that -command can be used with the name of a procedure instead of an in-line bracketed script. Notice also that line continuation characters are not required in command scripts, the lists enclosed by brackets. Read the entire script. How do you assign a variable in Tcl? Do you use parentheses to compute a simple mathematical operation? Notice that pack is used in the script twice. What would happen if you changed the script to read "pack .textvar .count" on a single line? When you have absorbed this script fully, let's examine what other widgets exist in Tk. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Step 9: Tk widgets
We have used the word, Widget, to refer to visual windowing components. This is not just slang, it is the official terminology in Tk (jargon) for a windowing gadget.
Review the information regarding scale. We'll be using it in the next exercise. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Step 10: Slider
Create a file called rgbscale.tcl containing the following lines. Source it into wish. We'll study it after we see what it does. Choose a color by sliding the scales. Notice that the button background is modified by the modify_color procedure. When you click the exit button (but not the X on the window), the program prints "Color is: <hex color string>" and terminates. FYI, HTML documents use the color number in the specified format, so this is a nice tool for picking exotic background and foreground colors for your web pages. --end of lesson-- |
What you have seen here is not programming. This is not computer code. This is a Tcl script interacting with Tk widgets. It is a software component glue with which (prior to Tcl) software developers have been dealing with in so much detail as to be unproductive. Do you know how long it would take a Microsoft C++ or Java programmer to write these kind of windows? The answer, my friend, is TOO LONG! Do you know how much studying, experience and skill it takes to even be able to produce such an application? WAY TOO MUCH! There are to many details. Too many things can go wrong! Even though I am a degreed computer scientist I recognize that using the Microsoft Foundation Classes and the object oriented C++ language to produce windows apps is JUST TOO MUCH PAIN. I previously did Visual Basic programming for a year and a half before getting into Linux/open source software and have since learned Perl, Javascript, PHP and HTML. These scripting languages provide so much more power, I will never go back.
Read more about it in John Ousterhout's IEEE article. I hope you are as excited about the possibilities as I am. This cool language is cross-platform meaning the same program can run on Windows, Linux, Macintosh or even in a browser window. |