Download presentation
Presentation is loading. Please wait.
Published byChristopher Simon Modified over 9 years ago
1
Innovation Intelligence ® 1 Chapter 1: Introduction to TCL
2
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 22 TCL/Tk Introduction About TCL About Tk Basic TCL Syntax TCL Command Overview Tk Basic Commands
3
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 33 TCL/Tk Introduction – About TCL and Tk About TCL Simple and programmable syntax Can be used as standalone application or embedded in programs Open source Interpreted language New TCL commands can be implemented using C language About Tk Graphical user interface (GUI) toolkit Tk adds about 35 TCL commands Create and manipulate widgets Widget is a window in GUI with particular appearance and behavior
4
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 44 TCL/Tk Introduction – Basic Tcl Syntax First item of a command line is a command name. The words in the command line are separated by one or more spaces. Words can be grouped with double quotes or curly braces. Commands are terminated with new line or semi-colon. A word starting with a dollar sign ($) must be a variable name. The string will be replaced by the value of the variable. Words enclosed within square brackets must be a legal Tcl command. The strings would be replaced by the results of evaluating the command.
5
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 55 TCL/Tk Introduction – TCL Command Overview Text Output Variables and Variable Substitution Expressions Command Substitution Comparisons and Loops Lists Arrays Strings Procedures Namespaces
6
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 66 TCL Command Overview – Text Output Print a string using puts command Input: puts HyperWorks Output: HyperWorks If string is more than one word, the string must be enclosed in either double quotes (“ “) or braces ({ }) Input: puts “This is an example with quotes” puts {This is an example with braces} Output: This is an example with quotes This is an example with braces
7
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 77 TCL Command Overview – Text Output TCL command terminated with newline or semicolon ; Comments are designated with # at beginning of line or after semicolon Input: # An example using a semicolon puts “This is line 1”; puts {This is line 2}; #Note after the ; Output: This is line 1 This is line 2
8
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 88 TCL Command Overview – Variables Variables do not need to be declared before using Create variables using set command Delete variables using unset command Variable substitution done using $ to access value stored in variable Input: set software "HyperWorks" puts "The software we are using is $software" Output: The software we are using is HyperWorks
9
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 99 TCL Command Overview – Variables Variable substitution inside strings Difference between using double quotes and braces Variable substitution with double quotes Using \ before variable results in literal value being printed Input: set Z Albany set Z_LABEL “The Capitol of New York is: “ puts “$Z_LABEL $Z” ; # Prints the value of Z puts “$Z_LABEL \$Z”; #Prints a literal $Z instead of the value of Z Output Albany The Capitol of New York is: The Capitol of New York is: Albany The Capitol of New York is: $Z
10
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 10 TCL Command Overview – Variables Variable substitution with braces Braces disable the substitution of variables with braces Input: puts "$Z_LABEL $Z" puts {$Z_LABEL $Z} Output: The Capitol of New York is: Albany $Z_LABEL $Z
11
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 11 TCL Command Overview – Variables Multiple variable substitution Input: set month 2 set day 3 set year 09 set date "$month:$day:$year" puts $date Output: 2:3:09 Using eval command Input: set foo “puts hi” eval $foo Output:hi
12
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 12 TCL Command Overview – Expressions Expressions are evaluated using expr command Mathematical and rational expressions allowed Expressions consist of operands, operators, parenthesis White space between these are allowed
13
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 13 TCL Command Overview – Expressions Operands can be: Numerical values (integer or floating-point) TCL variable using $ String enclosed in double quotes or braces TCL command enclosed in brackets Operators can be: - + ~ !Unary minus, unary plus, bit-wise NOT, logical NOT. * / % Multiply, divide, remainder. + -Add and subtract. >Left and right shift. =Boolean less, greater, less than or equal, and greater than or equal. == !=Boolean equal and not equal. &Bit-wise AND. ^Bit-wise exclusive OR. |Bit-wise OR. &&Logical AND. ||Logical OR.
14
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 14 TCL Command Overview – Expressions Rational Expression 0 is false; 1 is true Input: expr 0 == 1 Output: 0 Input: expr 1 == 1 Output: 1 Mathematical Expression Input: expr 4 + 5 Output: 9
15
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 15 Mathematical functions supported in expressions: Input: expr sin(2) Output: 0.909297 TCL Command Overview – Expressions abscoshlogsqrt acosdoublelog10srand asinexppowtan atanfloorrandtanh atan2fmodroundwide ceilhypotsin cosintsinh
16
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 16 TCL Command Overview – Command Substitution Square brackets [ ] are used to achieve command substitution Text between [ ] are evaluated and its result is substituted in its place Input: set my_height 6.0 puts "If I was 2 inches taller, I would be [expr $my_height + (2.0 / 12.0)] feet tall" Output: If I was 2 inches taller, I would be 6.16667 feet tall
17
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 17 TCL Command Overview – Comparisons & Loops Decision making commands If-else statements Switch statement Looping statements while for foreach Commands can alter the flow of execution in response to some condition
18
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 18 TCL Command Overview – Comparisons & Loops Using if-else statements if test1 body1 ?elseif test2 body2 elseif...? ?else bodyn? Input: set k 35 if {$k == 35} { puts "Handling is good." } elseif {$k == 20} { puts "Ride is good." } else { puts "I am not sure of the quality of ride or handling." } Output: Handling is good. Indentations are not required, but they make it easier to read the code
19
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 19 TCL Command Overview – Comparisons & Loops Using switch statements switch ?options? string {pattern body ?pattern body...?} Input: set num_legs 4 switch $num_legs { 2 {puts "It could be a human."} 4 {puts "It could be a cow."} 6 {puts "It could be an ant."} 8 {puts "It could be a spider."} default {puts "It could be anything."} } Output: It could be a cow.
20
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 20 TCL Command Overview – Comparisons & Loops Using for statements for init test reinit body Input: for {set i 0} {$i < 5} {incr i 1} { puts "In the for loop, and i == $i" } Output: In the for loop, and i == 0 In the for loop, and i == 1 In the for loop, and i == 2 In the for loop, and i == 3 In the for loop, and i == 4
21
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 21 TCL Command Overview – Comparisons & Loops Using while statements while test body Input: set i 0 while {$i < 5} { puts "In the while loop, and i == $i" incr i 1 } Output: In the while loop, and i == 0 In the while loop, and i == 1 In the while loop, and i == 2 In the while loop, and i == 3 In the while loop, and i == 4
22
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 22 TCL Command Overview – Comparisons & Loops Using foreach statements foreach varName list body Input: foreach vowel {a e i o u} { puts "$vowel is a vowel" } Output: a is a vowel e is a vowel i is a vowel o is a vowel u is a vowel
23
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 23 TCL Command Overview – Lists Simple way to group items Deal with the collection of items as a single entity Lists can be defined by: A string: set lst “item1 item2 item3” A variable to be a list of values set lst {{item1} {item2} {item3}} Using the split command set lst [split “item1.item2.item3” “.”] Using the list command set lst [list “item1” “item2” “item3”]
24
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 24 TCL Command Overview – Lists An individual member of list can be accessed with lindex Input: set simple_list “John Joe Mary Susan” puts [lindex $simple_list 0] puts [lindex $simple_list 2] Output: John Mary List indexing is zero-based Lists can be iterated through using the foreach command
25
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 25 TCL Command Overview – Lists The length of a list can be determined with llength Input: set names {Canada India USA UK} puts “List length is: [llength $names]” Output:List length is: 4 A value can be inserted into a list using linsert Input: set names {Canada India USA UK} set newnames [linsert $names 2 China] puts “New list is: $newnames” Output:New list is: Canada India China USA UK
26
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 26 TCL Command Overview – Lists A list can be appended to using the lappend command Input: set names {Canada India USA UK} set newnames [lappend names China] puts “New list is: $newnames” Output:New list is: Canada India USA UK China Notice how with lappend the name of a variable containing the list (names) is used rather than the list itself ($names).
27
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 27 TCL Command Overview – Arrays Unlike arrays in many other languages, Tcl arrays are indexed by keywords. The keywords can be easy to remember strings or integers, just as you like. The values of the array elements can also be strings or numbers. A Tcl array is created when you assign the first array element: Input: set myArray(foo) "bar" puts $myArray(foo) Output:bar The array command is used in Tcl to manipulate array data. Input: array set fruitColors {tomato red banana yellow} puts $fruitColors(tomato) Output:red
28
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 28 TCL Command Overview – Strings Strings are a set of alphanumeric characters stored and manipulated together All data items in Tcl, including numeric values, are treated as strings. They are treated as other data types only as needed. This makes string manipulation and the associated commands very important and frequently utilized. Input: set str "This is Canada" puts "The string is: $str" puts "The length of the string is: [string length $str]" puts "The character at index 3 is: [string index $str 3]" puts "The characters from index 3 through end are: [string range $str 3 end]" puts "The index of the first occurrence of letter \"i\" is: [string first i $str]“ Output: This is Canada The string is: This is Canada The length of the string is: 14 The character at index 3 is: s The characters from index 3 through end are: s is Canada The index of the first occurrence of letter “i” is: 2
29
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 29 TCL Command Overview – Procedures TCL procedures similar to functions in C Procedures may take arguments and may return values The basic syntax for defining a procedure is: proc name argList body Once a procedure is created, it is considered to be a command Called using its name, followed by a value for each of its arguments By default, the return value from a procedure is the result of the last command in its body However, to return another value, the return command may be used
30
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 30 TCL Command Overview – Procedures Input: proc sum_proc {a b} { return [expr $a + $b] } proc magnitude {num} { if {$num > 0} { return $num } set num [expr $num * (-1)] return $num } set num1 12 set num2 14 set sum [sum_proc $num1 $num2] puts "The sum is $sum" puts "The magnitude of 3 is [magnitude 3]" puts "The magnitude of -2 is [magnitude -2]" Output: The sum is 26 The magnitude of 3 is 3 The magnitude of -2 is 2
31
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 31 TCL Command Overview – Namespace A namespace is a collection of commands and variables. Create and manipulate contexts for commands and variables It encapsulates the commands and variables to ensure that they won't interfere with the commands and variables of other namespaces. Tcl has always had one such collection, which we refer to as the global namespace.
32
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 32 TCL Command Overview – Namespace The namespace eval command lets you create new namespaces. For example, namespace eval Counter { namespace export bump variable num 0 proc bump {} { variable num incr num } Creates a new namespace containing the variable num and the procedure bump. The commands and variables in this namespace are separate from other commands and variables in the same program. If there is a command named bump in the global namespace, for example, it will be different from the command bump in the Counter namespace.
33
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 33 TCL/Tk Introduction – Tk Basic Commands tk_getOpenFile tk_getSaveFile tk_chooseDirectory tk_messageBox
34
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 34 Tk Basic Commands - tk_getOpenFile tk_getOpenFile pops up a dialog box for the user to select a file to open Associated with the Open command in the File menu Its purpose is for the user to select an existing file only This dialog box does not open a file, it simply returns the filename so that it can be used in your script.
35
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 35 Tk Basic Commands - tk_getOpenFile Input: set filename [tk_getOpenFile] puts $filename Output: C:/Documents and Settings/training/My Documents/autosave.mvw
36
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 36 Tk Basic Commands - tk_getOpenFile Additional options can be used with the following format: tk_getOpenFile ?option value...? Example with option –filetypes set types { {{Text Files} {.txt} } {{TCL Scripts} {.tcl}} {{All Files} * } } set filename [tk_getOpenFile -filetypes $types]
37
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 37 Tk Basic Commands - tk_getSaveFile tk_getSaveFile pops up dialog box for user to select a file to save Associated with the Saveas command in the File menu This dialog box does not save a file, it simply returns the filename so that it can be used in your script Behaves the same way as tk_getOpenFile procedure Have the same options Example using –title option Input: set filename [tk_getSaveFile –title “Select a File”] puts $filename Output: C:/Documents and Settings/training/My Documents/test.txt
38
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 38 Tk Basic Commands - tk_chooseDirectory tk_chooseDirectory pops up a dialog box for the user to select a directory Allows user to set a directory name to a variable to be used in a script Input: set dirname [tk_chooseDirectory] puts $dirname Output: C:/temp
39
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 39 Tk Basic Commands - tk_messageBox tk_messageBox procedure creates and displays a message window with an application-specified message, an icon, and a set of buttons. The following example illustrates the tk_messageBox command with the message option. This option allows you to specify the message to be displayed in the message box. Input: tk_messageBox -message "This is the message to be displayed" Output:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.