1 / 19

TCL/TK

TCL/TK. Tool Command Language/Tool Kit. What is Tcl/Tk?. Tcl a scripting language can be extended in C (but this is harder) ugly but simple Tk a simple but powerful widget set Hello World: a complete program that exits when a person presses the button

ranker
Télécharger la présentation

TCL/TK

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TCL/TK Tool Command Language/Tool Kit

  2. What is Tcl/Tk? • Tcl • a scripting language • can be extended in C (but this is harder) • ugly but simple • Tk • a simple but powerful widget set • Hello World: a complete program that exits when a person presses the button • grid [ button .myButton -text "Hello World" -command exit ] • Simple things are simple, hard things are possible

  3. Tcl features • Tcl supports a lot of functions for • string processing • list processing • arrays (key–value pairs) processing It also supports error trapping, sockets, namespaces, packages and all that.

  4. Scripts and Commands • Tcl script = • Sequence of commands. • Commands separated by newlines, semi-colons. • Tcl command = • One or more words separated by white space. • First word is command name, others are arguments. • Returns string result. • Example: set myName Saul

  5. Variable Substitution • Syntax: $varName • Variable name is letters, digits, underscores. • May occur anywhere in a word. Sample commandResult set b 66 66 set a b b set a $b 66 set a $b+$b+$b 66+66+66 set a $b.3 66.3 set a $b4no such variable

  6. Command Substitution • Syntax: [script] • Evaluate script, substitute result. • May occur anywhere within a word. Sample commandResult set b 8 8 set a [expr $b+2] 10 set a "b-3 is [expr $b-3]" b-3 is 5

  7. Controlling Word Structure • Words break at white space and semi-colons, except: • Double-quotes prevent breaks: set x 4; set y 5 set a "x is $x; y is $y" -> x is 4; y is 5 • Curly braces prevent breaks and substitutions: set a {[expr $b*$c]} ->[expr $b*$c] • Backslashes quote special characters: set a word\ with\ \$\ and\ space ->word with $ and space

  8. Controlling Word Structure (continued) • Substitutions don't change word structure: • set a "two words" set b $a-> two words

  9. Comments • The # is the comment command • Tcl parsing rules apply to comments as well set a 22; set b 33 <- OK # this is a comment <- OK set a 22 # same thing? <- Wrong! set a 22 ;# same thing <- OK

  10. Summary of Tcl Command Syntax • Command: words separated by whitespace • First word is a function, others are arguments • Only functions apply meanings to arguments • Single-pass tokenizing and substitution • $ causes variable interpolation • [ ] causes command interpolation • “” prevents word breaks • { } prevents all interpolation • \ escapes special characters

  11. String Manipulation • String manipulation commands: regexp format split string regsub scan join • string subcommands compare first last index length match range toupper tolower trim trimleft trimright • Note: all indexes start with 0. end means last char • string tolower "THIS" ;# this • string trimleft “XXXXHello” ;# Hello • string index “abcde” 2 ;# c

  12. Control Structures • C-like in appearance. • Just commands that take Tcl scripts as arguments. • Commands: if for switch break foreach while eval continue

  13. if else set x 2 if {$x < 3} { puts "x is less than 3" } else { puts "x is 3 or more" }

  14. Procedures • proc command defines a procedure: proc decrement {x} { expr $x-1 } • Procedures behave just like built-in commands: decrement 3 í2 • Arguments can have default values: proc decrement {x {y 1}} { expr $x-$y } decrement 100 5 ;# 95 decrement 100 ;# 99 name body list of argument names

  15. Procedures • 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 sum í0

  16. Example • FACTORIAL WITH PROCEDURES proc factorial n { if {$n <= 0} { return 1 } else { return [expr {$n * [factorial [expr {$n - 1}]]}] } }

  17. TK widgets • To create a widget, you invoke a command named after the widget’s class: button for button widgets, scrollbar for scrollbar widgets, and so on.. • For example, the following command creates a button that displays the text “Press me” in red: • button .b -text "Press me" -foreground red • The widget name is followed by any number of pairs of arguments, • where the first argument of each pair specifies the name of a configuration option for the widget (e.g. -text or -foreground) • and the second argument specifies a value for that option (e.g. “Press me” or red).

  18. With wish • you can make a widget appear by invoking the pack command with the widget’s name as argument. • For example, the following script creates a button widget and displays it on the screen: • button .b -text "Hello, world!" • pack .b • This will size the main window so that it is just large enough to hold the button and it will arrange the button so that it fills the space of the main window.

  19. Tcl/Tk applications • TkDVI is a DVI previewer built with the Tcl/Tk toolkit. • http://tkdvi.sourceforge.net/ • TkVNC is a VNC viewer written in pure Tcl/Tk. It is designed to be embedded inside other applications, but can be used as a standalone VNC viewer. • http://www.ifost.org.au/Software/tkvnc/index.html • Xpvm is a TCL/TK based tool that allows full manageability of the PVM cluster as well as the ability to monitor cluster performance. • http://www.csm.ornl.gov/pvm/

More Related