TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable,...

10
TCL TK

Transcript of TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable,...

Page 1: TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,

TCL TK

Page 2: TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,

Tcl/Tk

• C functions can become Tcl commands that are invoked interactively

• Tk = scriptable, portable user interface– Windows, X (Unix), MacOS, MacOS X– also found in Python and Perl GUI extensions– scripts can send commands to each other

Page 3: TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,

Tcl history

• Developed in late 1980s by John Ousterhout

• first release ~1991

• Tk usable around 1992• see http://www.tcl.tk/doc/tclHistory.html

Page 4: TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,

Tcl/Tk features

• high-level scripting language– less code than Win32

• 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

Page 5: TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,

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

Page 6: TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,

Tcl/Tk script files

• Script file:#!/usr/local/gnu/bin/wish –fbutton .b –text "Hello, world!" \ –command exitpack .b

Page 7: TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,

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)

Page 8: TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,

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 commandswm withdraw .

console show

Page 9: TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,

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

Page 10: TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,

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

}