TCL TK TclTk C functions can become Tcl
TCL TK
Tcl/Tk • C functions can become Tcl commands that are invoked interactively • Tk = scriptable, portable user interface – Windows, X (Unix), Mac. OS X – also found in Python and Perl GUI extensions – scripts can send commands to each other
Tcl history • Developed in late 1980 s by John Ousterhout • first release ~1991 • Tk usable around 1992 • see http: //www. tcl. tk/doc/tcl. History. html
Tcl/Tk features • high-level scripting language – less code than Win 32 • interpreted – execute directly, without compiling or linking • extensible – commands in Tcl or C • embeddable – Tcl interpreter callable from C • most platforms – Unix, Mac, Windows – hides UI, system call differences • autoloading – automatically load libraries • free – source – no royalties
Using Tcl/Tk • Three modes: 1. tclsh for interactive use $ tclsh % set x 7 7 2. wish for window programs $ wish % button. b –text “Hello” –command exit % pack. b
Tcl/Tk script files • Script file: #!/usr/local/gnu/bin/wish –f button. b –text "Hello, world!" –command exit pack. b
Basic operations • print to screen (puts) puts –nonewline "Hello, world!" puts "!!" • assignment (set) set income 32000 puts "income is $income" (using '$' to get the value of a variable) • mathematical expressions (expr) set a 10. 0 expr $a + 5 expr int($a/3)
Some useful commands • unset: destroy a variable unset num • info: check whether the named variable has been defined if {![info exists num]} { set num 0 } incr num • window commands wm withdraw. console show
Special characters : single-line comments, similar to "//" in C : escape character, same function as in C : also used to break a long line of code to two lines $ : get the value of a variable # – var : name of variable – $var : value of variable [] : evaluate command inside brackets
Control structures • if then else set income 32000 if {$income > 30000} { puts "$income -- high" } elseif {$income > 20000} { puts "$income -- middle" } else { puts "$income -- low" } • while loops set i 0 while {$i < 100} { puts "I am at count $i" incr i }
- Slides: 10