Revision [696]

This is an old revision of ToolCommandLanguage made by CrustyLobster on 2009-08-30 22:49:57.

 

Programming in Tcl/Tk


This is a page for people interested in learning how to program using tcl and Tk
http://www.tcl.tk/ Lets get straight into Tool Command Language ( tcl ).
If you are in Puppy click on rxvt and type in

wish

you should have a window open on the desktop named wish and the # prompt should have changed to a %.

Now for the classic ' Hello World! ' program, type

puts "Hello World!" and hit enter

Hello World! should appear at the prompt.

Congratulations, you have just written your first tcl program.

To dissect this lets look at what happened.
First you opened the tcl wish shell and then entered a command ' puts'.
This prints to the standard output which is the terminal that you are working in.
What it outputs is the text enclosed by the " " .

Calculations are done with the command expr:
Type:
expr 1+2
and Tcl will respond with "3".

To force calculation in floating-point, add a decimal point:
expr 17/4.0

Assign and use a variable:
set x 17
puts "x=$x"


 set t1 Hello
set t2 "Good morning"
puts "$t1, $t2"


A complete, simple example:
 set x 17
set y 4.0
set z [expr $x/$y]
puts "$x/$y=$z"

The square brackets above are used to group the result that gets assigned to z.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

tcl/Tk more info

Tcl is an interpreted language which means that you do not have to run it through a compiler to create a program,
you can write a tcl program and run it to see the results immediately.
Tcl is composed of built in commands and commands you can create yourself called procedures
which use the name proc and are what you could call sub-programs or macros.

Here is an example of a small procedure that calculates the square of its argument,
and a loop to output a simple table:
 proc square {x} { return [expr $x*$x] }
for {set x 1} {$x <= 5} {incr x} {
set y [square $x]
puts "$x $y" 
}


Tk is the Tool kit used by tcl and is used to create "windows" to view and display information and graphics.
The main window that you create is represented by a period . and any frames or widgets created
inside that window start with a . so if a frame is created to contain text
you could name it something like .txt and any text entered would be referenced to .txt as in:
 package require Tk
text .txt
pack .txt
.txt insert end "Hello World!" 

which means define a text-frame named ".txt", use the packer to put it into the window .,
then insert the strings "Hello World!" at the end of the text frame.

To make this Tk-example somewhat interactive, we can add two buttons to the above code:
 button .but1 -text "Bla"   -command {.txt insert end "Bla\n"}
button .but2 -text "Quit" -command {exit}
pack .but1  .but2 


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Please have a look here for more, LearnTclTk


image

tcl cookbook
About TclTk, there is a highly recommended TclTutor, a very good starting point:

http://www.msen.com/~clif/TclTutor.html
http://wiki.tcl.tk/

Uses the tcl/tk to present tcl/tk source code learning, it is really really really worth the effort to download and install if you have any interest in understanding or using tcl/tk.

Here is a more up to date tutorial covering latest features:
http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html

Other Tcl/Tk Resources
Tcl/Tk Surprise * 5
tcl tutorial
wiki.tcl.tk Tclers' Wiki has a page devoted to online tutorials
comp.lang.tcl
Teach Programming to Children

Tcl Manual in other Languages, Dutch, Finnish, French, German .... (11 different languages)
Endekalogue

Reference card - also for C and Bash
See Tinycc for info about the tiny C compiler
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki