Skip navigation.

aleksanteri's blog

<insert a subtitle here>

Tcl/Tk - fast way to make platform independant (GUI) applications

, ,

So, in case you are finding C++ too hard, you can always try making some apps in Tcl/Tk.

Tcl has a very simple notation, where everything is treated as strings. This means there's no data types at all, there are strings and strings only. Also, everything's treated as a command.

Curly braces ({}) are kinda "absolute strings". Variables inside them are not substituted. Brackets ([]) are command substituters. Quoted strings ("") work in Tcl as in PHP, variables get subsituted.

Variables are set with [set] (do NOT use [$myvar="mystring"] it won't work p: ) and called with $var

Getting started


You should get ActiveTCL, or, if you are on Linux and have APT, you can do:
sudo apt-get install tcl8.4 tk8.4
To run the Tcl interactive shell, type tclsh for plain Tcl in a command line, or wish for Tk.

Basic commands


puts text
Prints text to the output

echo text
Behaves like [puts]

exit
Close the program

set varname value
sets $varname to $value

unset varname
unsets $varname

if expression command ?else command?
if expression resolves into "1" the command is executed. if [else command] is supplied, the command is executed if the expression doesn't resolve into "1"

while expression command
as long as expression resolves into 1, command is executed and loops

expr expression
executes a mathematical expression and returns the result

open file ?access? ?permission?
opens a file and returns the channel id. access can be one of the following: r r+ w w+ a a+.
r = read only (file must exist)
r+ = read and write (file must exist)
w = write only (file truncated if it has contents and created if doesn't exist)
w+ = read and write (file truncated if it has contents and created if doesn't exist)
a = append only
a+ = read and append

if file is created, you $permission is applied to it if given

close chid
closes a file stream

gets chid var
gets one line from chid and outputs it to var

Examples


set i 1
while {$i <= 5} {
 puts "$i"
 set i [expr $i + 1]
}

Counts from 1 to 5 and puts the numbers

set f [open myfile.txt r]
while 1 {
 if { [eof $f] } { close $f; exit; }
 gets $f out
 puts $out
}

Reads myfile.txt and outputs the contents

GUI


Tk is used for building GUIs for applications. Here's a simple Hello world! dialog written in Tcl/Tk:
package require Tk
frame .f
label .l -text "Hello World!"
button .btn -text "Close" -command exit

pack .l .f
pack .btn .f


frame creates a window frame, label initializes a text label, button initializes a button and pack packs them into the frame

Some useful links:
Tcl commands manual
Tk commands manual

For help, you can join the Tcl channel on Freenode: irc://irc.freenode.net/tcl
And off you go :smile:

LFSTagged

Comments

Anonymous 2. July 2008, 21:14

Anonymous writes:

Test

Anonymous 2. July 2008, 21:15

Anonymous writes:

Example 1: better (shorter) could be:

set i 1
while {$i

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies

January 2010
M T W T F S S
December 2009February 2010
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31