85 lines
2.9 KiB
HTML
85 lines
2.9 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
||
|
<HTML>
|
||
|
<HEAD>
|
||
|
<META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
|
||
|
<TITLE>The Linux Tcl and Tk HOWTO: Just a little bit of Programming</TITLE>
|
||
|
<LINK HREF="TclTk-HOWTO-6.html" REL=next>
|
||
|
<LINK HREF="TclTk-HOWTO-4.html" REL=previous>
|
||
|
<LINK HREF="TclTk-HOWTO.html#toc5" REL=contents>
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<A HREF="TclTk-HOWTO-6.html">Next</A>
|
||
|
<A HREF="TclTk-HOWTO-4.html">Previous</A>
|
||
|
<A HREF="TclTk-HOWTO.html#toc5">Contents</A>
|
||
|
<HR>
|
||
|
<H2><A NAME="s5">5. Just a little bit of Programming</A></H2>
|
||
|
|
||
|
<P>
|
||
|
<P>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:
|
||
|
<HR>
|
||
|
<PRE>
|
||
|
wish hello.tcl
|
||
|
</PRE>
|
||
|
<HR>
|
||
|
<P>You can also use wish in interactive mode and type in commands at the command
|
||
|
line.
|
||
|
<P>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.
|
||
|
<P>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 applicati
|
||
|
on
|
||
|
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 interpret
|
||
|
er,
|
||
|
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.)
|
||
|
<H2><A NAME="ss5.1">5.1 The one-minute program in Tcl</A>
|
||
|
</H2>
|
||
|
|
||
|
<P>
|
||
|
<P>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 if it was
|
||
|
a C function. This function is responsible for handling all the arguments.
|
||
|
<P>
|
||
|
<P>As a very standard example, the following is the Hello World program in
|
||
|
Tcl/Tk:
|
||
|
<HR>
|
||
|
<PRE>
|
||
|
# This is a comment
|
||
|
button .b -text "Hello World" -command exit
|
||
|
pack .b
|
||
|
</PRE>
|
||
|
<HR>
|
||
|
<P>In this case you have to type the commands interactively to tclsh or wish.
|
||
|
<P>You can also place command into script files and invoke these just like
|
||
|
shell scripts. To do this for the previous example, rewrite the Hello World
|
||
|
program as follows:
|
||
|
<HR>
|
||
|
<PRE>
|
||
|
#! /usr/local/bin wish -f
|
||
|
button .b -text "Hello World" -command exit
|
||
|
pack .b
|
||
|
</PRE>
|
||
|
<HR>
|
||
|
<P>Put the text inside a file called Hello and make sure that wish is installed
|
||
|
in /usr/local/bin (otherwise you will have to change opportunely the path).
|
||
|
<P>Make the file Hello executable issuing the command
|
||
|
<HR>
|
||
|
<PRE>
|
||
|
chmod 775 Hello
|
||
|
</PRE>
|
||
|
<HR>
|
||
|
<P>and run it inside X.
|
||
|
<P>You will see a button labelled Hello World inside a window: clicking it
|
||
|
will close (exit) the window.
|
||
|
<HR>
|
||
|
<A HREF="TclTk-HOWTO-6.html">Next</A>
|
||
|
<A HREF="TclTk-HOWTO-4.html">Previous</A>
|
||
|
<A HREF="TclTk-HOWTO.html#toc5">Contents</A>
|
||
|
</BODY>
|
||
|
</HTML>
|