| Lecture Topics in HCI, by Saul Greenberg | |
| Back to: | |
Most code fragments are included in this file. You can just cut and paste between the file and the wish interpretter to try things out.
What is it? 
- Scripting language, where basic functionality can be extended in C
 - Tcl=dynamic, string substitution-based interpreted script language
 - Tk=high-level cross-platform UI toolkit (callable from Tcl, Perl, Python, ...)
 Why Tcl? 
- free
 - quick to learn
 - documented
 - good set of powerful widgets (especially the canvas and text widget)
 - used (somewhat) in industry
 How to 
use it
- from your PC
 - Start / Tcl / Wish to run the tcl/tk interpretter
 
Start / Tcl / Help to see the on-line manual
Start / Tcl / Widget tour to see examples of Tk Widgets and codeResources 
- Visit the Scriptics site http://www.scriptics.com/
 - you can find widget extensions at http://www.scriptics.com/resource/software/extensions/tk/
 Download 
- Available as a binary file for both Mac and Windows for free!!!
 - We have a local copy for windows (about 1.7 Mb) or
 - get it from Scriptics http://www.scriptics.com/software/8.0.html
 - and this is a plug-in (somewhat restricted) version that runs within a web browser
 
Scripts and 
commands
- Tcl Script
 
- a sequence of commands
 - separated by semi-colons and / or new lines
 - Tcl command
 
- One or more words separated by white space.
 - First word is command name, others are arguments. Returns string result.
 - Example script
 
- set myName Saul
 
puts "My Name is $myName and I teach "
set class CPSC-481; puts -nonewline $classArguments 
- Parser assigns no special meaning to arguments
 
- set x 4
 - set y x+4
 - set z $x+4
 - "1+1"
 - Different commands assign different meanings to their arguments. Type-checking must be done by commands themselves.
 
- expr 24/3.2
 - eval "set a 122"
 - button .b -text Hello -fg red
 - string length Abracadabra
 
Variable 
substitution
- Syntax: $varName
 - Variable names are made of letters, digits, and underscores. Variables may be substituted anywhere within other words.
 
- set b 66 -> 66
 
set a b -> b
set a $b -> 66
set a $b.3 -> 66.3
set a $b4 -> no such variableCommand 
substitution
- Syntax: [script]
 - Evaluate script, substitute result. May occur anywhere within a word.
 
- set b 8 -> 8
 
set a [expr $b+2] -> 10
set a "b-3 is [expr $b-3]" -> b-3 is 5Controlling 
word
structure
- Words normally break at white space and semi-colons
 - Double-quotes prevent breaks:
 
- set a "x is $x; y is $y" ->
 - Curly braces prevent breaks and substitutions:
 
- set a {[expr $b*$c]}
 - Backslashes quote special characters:
 
- set a word\ with\ \$\ and\ space
 - Backslashes can escape newline (continuationn)
 
- set aLongVariableNameIsUnusual \
 
"This is a string"- Substitutions don't change word structure:
 
- set a "two words"
 - set b $a
 Comments 
- Preceeded by the # sign
 - But follows same rules for parser!
 
- # this is a comment <- OK
 - set a 22 # this is a comment <- Wrong!
 - set a 22 ;# this is a comment <- OK
 
Math & 
expressions
- Arguments are interpretted as expressions in some commands: expr, if, ...
 - See help on expr
 
- set b 5 ;# 5
 
expr ($b*4)-3 ;# 17
expr $b <= 2 ;# 0
expr {$b * cos(4)} ;# -3.268...
set a Bill ;# Bill
expr {$a < "Anne"} ;# 0
expr {$a < "Fred"} ;# 1
Arrays 
- Associative arrays; index is any string
 - Assigments are done using the set statement,
 - You can fake multi-dimensional arrays
 
- set foo(fred) 44 ;# 44
 
set foo(2) [expr $foo(fred) + 6] ;# 50
array names foo ;# fred 2- set A(1,1) 10
 
set A(1,2) 11
array names A ;# 1,1 1,2 (note commas!)Lists 
- Zero or more elements separated by white space:
 - Braces and backslashes for grouping:
 
- set colors {red green blue}
 
set hierarchy {a b {c d e} f }- List-related commands:
 
concat lindex llength lsearch foreach linsert lrange lsort lappend list lreplace- Note: all indices start with 0. end means last element
 
- lindex $hierarchy 2 ;# c d e
 
lsort $colors ;# blue green redStrings 
- String manipulation commands:
 
regexp format split string regsub scan join- string subcommands
 
compare first last index length match range toupper tolower trim trimleft trimright- all indexes start with 0. end means last char
 
- string tolower "THIS" ;# this
 - string trimleft "XXXXHello" X ;# Hellow
 - string index "abcde" 2 ;# c
 
General 
- C-like in appearance.
 - Just commands that take Tcl scripts as arguments.
 - includes:
 
if for switch break foreach while eval continue sourceif else 
- set x 2
 
if {$x < 3} {
puts "x is less than 3"
} else {
puts "x is 3 or more"
}while 
- # list reversal
 
set a {a b c d e}
set b ""
set i [expr [llength $a] - 1]
while {$i >= 0} {
lappend b [lindex $a $i]
incr i -1
}
puts $bfor 
- for {set i 0} {$i<10} {incr i} {
 
puts $i
}
foreach 
- foreach color {red green blue} {
 
puts "I like $color"
}- set A(1) a; set A(2) b; set A(26) z
 
foreach index [array names A] {
puts $A($index)
}switch 
- set pete_count 0
 
set bob_count 0
set other_count 0
foreach name {Peter Peteee Bobus Me Bobor Bob} {
switch -regexp $name {
^Pete* {incr pete_count}
^Bob|^Robert {incr bob_count}
default {incr other_count}
}
}
puts "$pete_count $bob_count $other_count"
proc 
- proc name args body
 - proc commands defines a procedure
 
- proc decrement {x} {
 
expr $x-1
}- Procedures behave just like built-in commands:
 
- decrement 3
 args 
- Arguments can have defaults:
 
- proc decrement {x {y 1}} {
 
expr $x - $y
}- Procedures can have a variable number of arguments
 
- proc sum args {
 
set s 0
foreach i $args {
incr s $i
}
return $s
}
sum 1 2 3 4 5 ;# 15- Recursive procedures are ok
 
- proc fac x {
 
if $x==1 {return 1}
expr $x*[fac [expr $x-1]]
}
fac 4scoping 
- there are local and global variables
 - global variables used in procedures must be declared as such
 
- set outside "I'm outside"
 
set inside "I'm really outside"
proc whereAmI {inside} {
global outside
puts $outside
puts $inside
}
whereAmI "I wonder where I will be"
General 
- Tcl file I/O commands
 - open seek file close tell glob
 
gets flush cd read eof pwd
puts sourceopening, 
writing,
closing
- Similar to C
 
- set f [open "myfile.txt" "r"]
 
=> file4
puts $f "Write this text into file"
close $fgets, puts, 
read
- gets and puts are line oriented
 
- set x [gets $f] ;#reads one line of $f into x
 - read can read specific numbers of bytes
 
- read $f 100 ;# up to 100 bytes of file $f)
 sockets 
- Easy to program compared to C
 - example: get the first 100 characters from the departments home page
 - set f [socket www.cpsc.ucalgary.ca 80]
 
fconfigure $f -buffering line
puts $f "GET /"
puts [read $f 100]
close $fsource 
- source some TCL code
 - treated as if its in-line (e.g., like a #include in C)
 
- source myTclCode.tcl