Tcl/Tk - fast way to make platform independant (GUI) applications
Sunday, 15. July 2007, 12:51:36
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
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.4To 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
Test
By anonymous user, # 2. July 2008, 21:14:08
Example 1: better (shorter) could be:
set i 1
while {$i <= 5} {
puts "$i"
incr i
}
or even:
set i 1
time { puts $i ; incr i } 5
-----------------------------------
Example 2:
set f [open myfile.txt r]
puts [read $f]
close $f
----------------------------
Example 3 - the frame is completely unused currently. My proposal:
package require Tk
pack [label .l -text "Hello World!"]
pack [button .btn -text "Close" -command exit]
Shorter - but making exactly the same.
By anonymous user, # 2. July 2008, 21:15:28