Tcl and Tk presentation

Download Report

Transcript Tcl and Tk presentation

Tcl and Tk
Chen Wei, Gingko Studio
Content
history and purpose
Important Features
Tcl Syntax
Tk Examples
Content
history and purpose
Important Features
Tcl Syntax
Tk Examples
How it started
Codes must be modified when the environment
is changed, so a lot of time is wasted.
Find a programming language which can be:
1. Reused
2. Extended
3. Learned quickly
Solution: Component Approach
What is Tcl?
 developed by John Ousterhout
(OH-stir-howt) at UC Berkeley.
 Tcl stands for “tool command language”
 interpreted scripting language;
 most commonly used to build
graphical interfaces in conjunction
with Tk.
Cont… What is Tcl?
 The Tcl interpreter has been ported from UNIX to
DOS, Windows, OS/2, NT, and Macintosh
environments.
 The Tk toolkit has been ported from X Window to
Windows and Macintosh.
What is Tk?
 A graphical toolkit that gives one access to many of
the graphics primitives of the underlying windowing
system, as well as providing access to many types of
widgets, such as buttons, scrollbars, menus, and text
boxes.
 a set of commands and procedures that make it
relatively easy to program graphical user interfaces in
Tcl.
 Tk defines Tcl commands that let you create and
manipulate user interfaces.
What is tclsh and wish?
tclsh is an interpreter which interprets tcl
commands
wish is an interpreter that one can use that:
1)Interprets tcl commands
2)Interprets tk commands
Content
history and purpose
Important Features
Tcl Syntax
Tk Examples
Features
 Easy to learn
 Standard systax
 Graphical interfaces
 Cross-platform applications
 Rapid development
 Extensive and embeddable
 Flexible integration
 Free
Content
history and purpose
Important Features
Tcl Syntax
Tk Examples
Basics
 Tcl script =
– Sequence of commands.
– Commands separated by newlines, semicolons.
 Tcl command =
– One or more words separated by white space.
– First word is command name, others are arguments.
– Returns string result.
 Examples:
set a 22
puts "Hello, World!"
Tcl: Tool Command Language
 Simple syntax (similar to sh):
set a 47
 47
 Substitutions:
set b $a
 47
set b [expr $a+10]  57
 Quoting:
set b "a is $a"
 a is 47
set b {[expr $a+10]} 
[expr
$a+10]
More On The Tcl Language
 Rich set of built-in commands:
– Variables, associative arrays, lists.
– C-like expressions.
– Conditionals, looping:
if "$x < 3" {
puts "x is too small"
}
– Procedures.
– Access to files, subprocesses, network sockets
More On The Tcl Language
 Only data representation is zero-terminated
strings:
– Easy access from C.
– Programs and data interchangeable.
set cmd1 "exec notepad"
...
eval $cmd1
(notepad.exe launches under Windows)
Example: Factorial
proc fac x {
if {$x<=1} {
return 1
}
expr {$x*[fac [expr $x-1]]}
}
fac 4
Returns:
24
Content
history and purpose
Important Features
Tcl Syntax
Tk Examples
Hello World
pack [button .b -text "Hello" -command {puts "Hello World!"}]
 This shows a Hello button on the Main window.
 When you click the button the console window will
display the string "Hello World!" on the console.
 button command creates an instance .b button
called .hello.
 -text string shows the button title.
 -command string is the command responded to the
button's action.
 pack command layouts the button on the window.
Browser Wish Script
scrollbar .h -orient horizontal -command ".list xview"
scrollbar .v -command ".list yview"
listbox .list -selectmode single -width 20 -height 10 \
-setgrid 1 -xscroll ".h set" -yscroll ".v set"
label .label -text "File Selected:" -justify left
entry .e -textvariable fileSelected
Cont… Browser Wish Script
 grid .list -row 0 -column 0 -columnspan 2 -sticky "news"
 grid .v -row 0 -column 2 -sticky "ns"
 grid .h -row 1 -column 0 -columnspan 2 -sticky "we"
 grid .label -row 2 -column 0
 grid .e -row 3 -column 0 -columnspan 3 -sticky "we"
 grid columnconfigure . 0 -weight 1
 grid rowconfigure . 0 -weight 1
Cont… Browser Wish Script
foreach file [glob *] {
.list insert end $file
}
bind .list <ButtonRelease-1> \
{global fileSelected;set fileSelected \
[%W get [%W curselection]]}
The End