|
|
|
Graphical Applications with Tcl and Tk
An embeddable scripting language
|
Tcl
|
|
Tcl, pronounced
tickle, stands for the Tool Command Language.
With its associated user interface toolkit, Tk, pronounced tee-kay,
you can quickly create cross-platform applications with graphical user
interfaces, all without having to learn GTK, Qt, the Windows API, or Mac OS X§.
Created by John Ousterhout, Tcl is more like a scripting
language than a programming language, so it shares a greater
similarity to the Bash shell or Perl than it does to C++ or C.
For example, the following is the Hello World program in Tcl/Tk:
# This is a comment.
button .b -text "Hello World" -command exit
pack .b
My book, Graphical Applications with Tcl and Tk,
covers writing desktop applications with Tcl and Tk.
|
|
One-minute introduction
|
|
Tcl has a simple structure. Each line starts out with a command, such
as button and a number of arguments. Each command is implemented
as a C function (or Java method). This function is responsible for handling all the
arguments.
| Syntax |
Means |
|
command arg1 arg2
|
Execute the command, with the given arguments.
| |
"Text in quotes"
|
Pass the text between the quotes as one argument, performing
command and variable substitution.
| |
{Text in braces}
|
Pass the text between the quotes as one argument, defer substitution
until later.
| |
$variable
|
Substitute the value of the given variable.
| |
[command arg]
|
Execute the command, then substitute the return value in place
of the entire command between the square brackets.
|
command arg1 \
arg2
|
Extend command over one line.
|
Tcl commands can span multiple lines through the use of a line continuation
marker, \, or curly braces, { and }.
You can create your own commands in C, Java, or you can use the Tcl proc
command to create procedures written in Tcl.
|
|
|
Key features
|
|
- Tcl is a high-level scripting language.
- You'll find you need to write a lot less code to get your
job done, especially when compared to GTK or Windows applications.
- Tcl is interpreted.
- You can execute your code directly, without compiling and
linking (though Tcl compilers are available).
- Tcl is extensible.
- It's very easy to add your own commands to extend the Tcl
language. You can write your commands in C or Tcl (or Java and
Tcl with the Jacl interpreter).
- Tcl is embeddable in your applications.
- The Tcl interpreter is merely a set of C functions that
you can call from your code. This means you can use Tcl as an
application language, much like a macro language for a
spreadsheet application.
- Tcl runs on many platforms.
- Versions exist for UNIX, Linux, Windows, and Mac OS X platforms.
Except for a few platform
differences, your Tcl scripts run the same on all systems.
- Tcl's auto-loading facility makes for smaller applications.
- Tcl will automatically load in your libraries of Tcl
procedures (with one line of code to set up the path to
your library). Tcl will also, on systems that support it,
automatically load dynamic (shared, DLL) libraries when needed.
This is very handy.
- Tcl is free.
- Yep. You can get the sources for free over the Internet.
(See the links on the top left of this page.)
|
|
Running Tcl programs
|
|
Since Tcl is an interpreted language, to run a Tcl program (also called
a script), you normally pass the script file to the Tcl interpreter,
wish, for example:
wish hello.tcl
You can also use wish in interactive mode and type in commands
at the command line.
There's another standard Tcl interpreter, tclsh, which only understands
the Tcl language. tclsh does not have any of the Tk
user interface commands, so you cannot create graphical programs
in tclsh.
Some Tcl freeware applications extend the Tcl language by adding new commands
written as C functions. If such is the case, you need to compile the
application instead of just passing its Tcl code to the wish
interpreter. This application program, from a Tcl perspective, is really
a new version of the wish interpreter, which the new C commands
linked in. Of course, the application program may be a lot more than
merely a Tcl interpreter. (Note: you can also use Tcl's auto-loading
capability on systems that support it.)
|
|
|
Extensions
|
|
Since Tcl is so easy to extend, many try to share extensions, including
the popular itcl, [incr Tcl], ObjectTcl,
TclX, Tix and BLT.
These extensions, of course, require an extended Tcl interpreter. In addition,
many Tcl freeware applications require a particular Tcl extension to run.
One very popular extension is called
Expect, which allows you to
place a friendly front-end onto most command-line based UNIX applications,
such as telnet, ftp, passwd, fsck, rlogin, tip and so on.
|
|
Starkits and Starpacks
|
|
One of the most exciting developments in Tcl help you package your applications
into simple bundles. A tclkit is a one-file executable Tcl package
for a given system. The normal Tcl distribution includes a large number of
files that must be installed on each system. With a tclkit, though, you have
just one file, containing everything.
A starkit acts similarly, providing a single-file bundle of all
of your code for an entire Tcl application. In most cases, starkits
work on any platform Tcl does. (Starkits are similar to Jar files for
Java applications.)
You can combine a tclkit (the executable engine) and a starkit (your
application code) into a starpack, a single-file executable
of an entire application. With a starpack, you have an easy-to-install,
easy-to-transport Tcl application. Furthermore, because a starpack is
an executable program, your users don't even have to know this is
a Tcl application.
|
|
|
|