Tcl / Tk Tutorial

27
Tcl/Tk Tutorial Dongsoo S. Kim [email protected]

description

Tcl / Tk Tutorial. Dongsoo S. Kim [email protected]. What is Tcl / Tk ?. Tcl : T ool C ommand L anguage Interpreted programming (scripting) language Build on-the-fly commands and procedures Embeddable Platform-independent Tk : GUI toolkit and widgets based on Tcl - PowerPoint PPT Presentation

Transcript of Tcl / Tk Tutorial

Tcl/tk tutorial

Tcl/Tk TutorialDongsoo S. [email protected] is Tcl/Tk?Tcl: Tool Command LanguageInterpreted programming (scripting) languageBuild on-the-fly commands and proceduresEmbeddable Platform-independentTk:GUI toolkit and widgets based on TclOpen source, cross-platform 2Why Tcl/TK?Easy and fast programmingFreeLots of online documentation, mostly freeResourceshttp://www.tcl.tkMajor applications use the Tcl as their interfaces:Ns2Mentor Graphics3Hello World% tcltcl>puts "Hello. World!"Hello. World!tcl>exit% 4Script File Start the Tcl program followed by a filename containing the script.

% cat hello.tclputs "Hello. World!"

% tcl hello.tclHello. World!%

5Script as a ProgramA script text file can be considered as a program ifit is an executable file, andits first line contains a full path name of the tcl program following #!

% ls -l -rwxr-xr-x 1 user group 42 Jul 3 13:12 hello.tcl% which tcl/usr/local/bin/tcl% cat hello.tcl#!/usr/local/bin/tclputs "Hello. World!"% hello.tclHello. World!%6Tcl SyntaxSimple syntaxcommand_name arg1 arg2 Print to screen (puts)puts nonewline "Hello. "puts "World!"Assignment (set)set income 32000puts "The income is $income"Use '$' to get the value of a variable (R-value)7CommentsA line started with a pond sign(#) is ignored as comments until the end of the lineA command can be followed by a comment in a line with by placing ;# Exception#! at the first line indicates the script programIt is a Unix rule, but not a Tcl rule

# the first scriptputs nonewline "Hello. " ;# continuingputs "World!8Multiple Commands in a LineA Tcl command is terminated by an invisible new line character, orA semicolon(;) can be used to explicitly terminate a command

tcl>puts "Hello. "; puts "World! "helloworld

9A Command Stretched to LinesA command can be stretched to multiple lines by using backslashes (\)The backslash and a following new line character is ignoredNote: There must not be any characters (even space characters) between the backslash and the new line character

tcl>puts -nonewline \ =>"Hello. World!"Hello. World!tcl>10Mathematical ExpressionMathematical expression command (expr)Operatorsarithmetic (+, -, *, /, %)Bitwise (~, , &, ^, |)Logical (!, &&, ||)Boolean (, =, ==, !=)String Boolean (eq, ne, , =)List containment (in, ni)Ternary (x?y:z) Math functions (log, sin, cos, ) 11Tcl>set pi 3.141592Tcl>set r 2Tcl>expr 2*$pi*$r 12.566368Tcl>expr sin($pi/3)0.866025294853

Sub-Command EvaluationA Tcl command in a Tcl command: []

12tcl>set a 3.0tcl>set b $a+4tcl>puts $b3.0+4tcl>set c [expr $a+4]tcl>puts $c7.0Control Structures (if)if {cond} {commands}[ elseif {cond} {commands} ][ else {commands} ]

13set sel 3 if {$sel == 1} { puts "Selection $sel" } elseif {$sel == 2} { puts "Selection $sel" } else { puts "Invalid selection" }

Control Structures (while)while {cond} {commands}

14 set i 1 set sum 0 while {$i