Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Tcl/Tk: An introduction D. Kim, K. Kundu, and M. Siegel


Published on

Published in: Technology
  • Be the first to comment

Tcl/Tk: An introduction D. Kim, K. Kundu, and M. Siegel

  1. 1. Tcl/Tk: An introduction D. Kim, K. Kundu, and M. Siegel November 26, 2002 CMSC 631
  2. 2. Tcl ( Tool Control Language ) history <ul><li>Developed in late 1980s by John Ousterhout at UC Berkeley </li></ul><ul><li>Created as a single language used to control IC tools, rather than use a different language for each one. </li></ul><ul><li>Provides for extensions such as Tk (GUI), [incr Tcl] (OOP), etc. </li></ul>
  3. 3. Timeline of Tcl 8.0 Aug 1988 1989 1990 1991 1992 1993 1995 1997 1998 <ul><li>2. Open source distributions </li></ul><ul><li>from U.C. Berkeley: </li></ul><ul><li>Easy GUIs under Unix </li></ul><ul><li>Extensible applications </li></ul><ul><li>3. Tcl enhanced at Sun Microsystems: </li></ul><ul><li>Windows, Macintosh ports </li></ul><ul><li>Web/Internet support </li></ul><ul><li>Java support </li></ul><ul><li>4. Scriptics formed: </li></ul><ul><li>Evolve and extend Tcl platform </li></ul><ul><li>Create development tools </li></ul>1. Tcl created as general-purpose command/scripting language by John Ousterhout 1994 1999 1996 2000 2001 7. ActiveState introduces Tcl support and services 6.0 Sept 7.0 Sept 7.4 July 7.6 Oct 8.1 Apr 8.2 Aug 8.3 Feb … Slide courtesy of ActiveState
  4. 4. Installing Tcl/Tk <ul><li>Windows/Mac : Latest distribution maintained by ActiveState (ActiveTcl Download at </li></ul><ul><li>Unix/Linux : Tcl/Tk is included with most Unix/Linux distributions </li></ul>
  5. 5. 3 Ways to Use Tcl/Tk <ul><ul><li>tclsh for interactive use </li></ul></ul><ul><ul><ul><li>$ tclsh </li></ul></ul></ul><ul><ul><ul><li>% puts “I am using tclsh” </li></ul></ul></ul><ul><ul><ul><li>I am using tclsh </li></ul></ul></ul><ul><ul><li>wish for programs using the Tk package </li></ul></ul><ul><ul><li>Embed in C program with <tcl.h> </li></ul></ul>
  6. 6. Basics <ul><li>Tcl script = </li></ul><ul><ul><li>Sequence of commands . </li></ul></ul><ul><li>Tcl command = </li></ul><ul><ul><li>One or more words separated by white space. </li></ul></ul><ul><ul><li>First word is command name , others are arguments . </li></ul></ul><ul><ul><li>Returns string result . </li></ul></ul><ul><li>Example: </li></ul><ul><ul><li>set a 22 set b 33 </li></ul></ul>
  7. 7. [ ] and $ substitution <ul><li>Substitutions: </li></ul><ul><ul><li>variable substitution: </li></ul></ul><ul><ul><li>set id 631 </li></ul></ul><ul><ul><li>puts “This class is CMSC $id” </li></ul></ul><ul><ul><li>command substitution, evaluated as separate script: </li></ul></ul><ul><ul><li>set b [expr $id*4] </li></ul></ul>
  8. 8. Math Evaluation <ul><li>expr command evaluates expressions. </li></ul><ul><li>Sample command Result </li></ul><ul><li>set b 5 5 </li></ul><ul><li>expr ($b*4)-3 17 </li></ul><ul><li>expr $b <= 2 0 </li></ul><ul><li>Many other math functions included, such as sin , cos , sqrt , and log . </li></ul>
  9. 9. Conditional/Looping Statements <ul><li>Like most languages, Tcl supports an if statement, though the keywords then and else are optional. </li></ul><ul><li>For loop: </li></ul><ul><ul><li>for {set a 0} {$a < 100} {incr a} { </li></ul></ul><ul><ul><ul><li>#more code here </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>While loop is also supported </li></ul>
  10. 10. Tcl lists <ul><li>list are ordered collections of elements </li></ul><ul><li>any proper list can also be a Tcl command ( eval ) </li></ul><ul><li>concat list list – concatenate lists </li></ul><ul><ul><li>concat {a b c} {d e f}  a b c d e f </li></ul></ul><ul><li>join list sep – convert to string with separator </li></ul><ul><li>join {a b c} &quot;, &quot;  a, b, c </li></ul><ul><li>Some list functions : lappend lindex, linsert, llength, lrange </li></ul>
  11. 11. Tcl Arrays <ul><li>Tcl arrays are 'associative arrays': index is any string </li></ul><ul><ul><li>set nicholas(1) 331 </li></ul></ul><ul><ul><li>set nicholas(2) [expr $nicholas(1) + 300] </li></ul></ul><ul><ul><li>array names nicholas </li></ul></ul><ul><li>Matricies can be “faked” with index notation: </li></ul><ul><ul><li>set A(1,1) 10 </li></ul></ul><ul><ul><li>set A(1,2) 11 </li></ul></ul><ul><ul><li>array names A </li></ul></ul><ul><li>=> 1,1 1,2 ( commas included in names! ) </li></ul>
  12. 12. Regular Expressions <ul><li>Tcl has full support for regular expression pattern matching and substitution </li></ul><ul><li>regexp command for matching, places matched chars into variable specified </li></ul><ul><li>regsub for substitution </li></ul>
  13. 13. Tk : An Introduction <ul><li>Tk is a Toolkit for programmable user interfaces. </li></ul><ul><li>Tk provides a set of Tcl commands that create and manipulate widgets . </li></ul><ul><li>John Ousterhout began work on Tk in late 1988; finished in 1990. </li></ul><ul><li>Tk's GUI facilities were both very simple and very powerful. </li></ul>
  14. 14. Tk : Widgets <ul><li>A widget is window in a GUI with particular appearance and behavior. </li></ul><ul><li>Widget types include buttons, scrollbars, menus, and text windows. </li></ul><ul><li>Tk also has a general purpose drawing widget called a canvas that lets you create lightweight items such as lines, boxes and bitmaps. </li></ul>
  15. 15. Tk : Widgets (Con’t) <ul><li>Tk widgets organized in a hierarchy. - children windows inside a parent window </li></ul><ul><li>Parent widgets use frame widgets to lay the children windows out </li></ul><ul><li>Can create complex windowing schemes using Tk widget hierarchy. </li></ul>
  16. 16. Tk : Geometry Manager <ul><li>Widgets are under the control of geometry manager that controls their size and location on the screen </li></ul><ul><li>Until the GM learns about a widget, it is not mapped onto a screen </li></ul><ul><li>Types of Geometry Managers: </li></ul><ul><ul><li>The Pack GM </li></ul></ul><ul><ul><li>The Grid GM </li></ul></ul><ul><ul><li>The Place GM </li></ul></ul>
  17. 17. Tk : A Small Example <ul><li>#!/usr/local/bin/wish </li></ul><ul><li>button .hello -text Hello -command {puts stdout &quot;Hello World!&quot;} </li></ul><ul><li>pack .hello -padx 20 -pady 10 </li></ul><ul><li>(code courtesy of Practical Programming in Tcl and Tk by Brent B. Welch) </li></ul>
  18. 18. Tk : Events <ul><li>Tk-based application has event-driven control flow. </li></ul><ul><li>Usually Tk widgets handle most events automatically. </li></ul><ul><li>For specialized behavior, bind command is used. </li></ul>
  19. 19. Tk : Events (Con’t) <ul><li>Examples of events include mouse motion, mouse clicks, keystrokes, window resizing, window destruction </li></ul><ul><li>Virtual events like cut and paste are also possible. </li></ul><ul><li>Event bindings grouped into classes called bindtags which are associated with a class. </li></ul><ul><li>Focusing on windows helps switch bindtags. </li></ul>
  20. 20. Example – diff command #!/usr/bin/wish # Description : # this program will give the user a graphical interface to the Unix # command &quot;diff&quot;. The window will allow the user to specify a pair # of files to check for differences and a few options, as well as # colorizing the output appropriately # title wm title . tkdiff
  21. 21. Example: (Con’t) # frame for the first file frame .first -borderwidth 1 label .first.name1 -text &quot;Filename 1:&quot; -foreground red entry .first.ent1 -width 68 -relief sunken -textvariable name1 pack .first.name1 -side left pack .first.ent1 -side left -fill x -expand true
  22. 22. Example: (Con’t) # frame for the second file frame .second -borderwidth 0 label .second.name2 -text &quot;Filename 2:&quot; -foreground blue entry .second.ent2 -width 68 -relief sunken -textvariable name2 pack .second.name2 -side left pack .second.ent2 –side left -fill x -expand true pack .first .second -fill both
  23. 23. Example: (Con’t) # frame for check boxes set third [frame .third -borderwidth 2] checkbutton $third.1 -text &quot;Ignore Case Changes&quot; -variable cFlag checkbutton $third.2 -text &quot;Ignore Whitespace Diffs&quot; -variable wFlag pack $third.1 $third.2 -side left pack $third -fill x
  24. 24. Example: (Con’t) # frame for command buttons set fourth [frame .four -borderwidth 2] button $fourth.1 -text Quit -command Exit $fourth.1 config -activebackground red button $fourth.2 -text Go -command Run $fourth.2 config -activebackground green button $fourth.3 -text Clear -command Clear $fourth.3 config -activebackground blue pack $fourth.1 $fourth.2 $fourth.3 -side left pack $fourth
  25. 25. Example: (Con’t) # frame for display area frame .msg -borderwidth 2 set box [text -width 60 -height 10 -borderwidth 1 -relief raised -setgrid true -yscrollcommand {.msg.yscroll set} -xscrollcommand {.msg.xscroll set}] scrollbar .msg.yscroll -command { yview} -orient vertical scrollbar .msg.xscroll -command { xview} -orient horizontal pack .msg.yscroll -side right -fill y pack .msg.xscroll -side bottom -fill x pack -side left -fill both -expand true pack .msg -side top -fill both -expand true
  26. 26. Example: (Con’t) # for change colars tag configure TagA -foreground red tag configure TagB -foreground blue # when user click exit button proc Exit {} { set picked [tk_messageBox -type yesno -message &quot;Really Quit?&quot; -default no -icon question] if {$picked == &quot;yes&quot;} { exit } }
  27. 27. Example: (Con’t) # when user click go button proc Run {} { global box name1 name2 cFlag wFlag input if {$wFlag == 1 && $cFlag == 1} { set cmd &quot;diff $name1 $name2 -c -w&quot; } elseif {$wFlag == 1 && $cFlag == 0} { set cmd &quot;diff $name1 $name2 -w&quot; } elseif {$wFlag == 0 && $cFlag == 1} { set cmd &quot;diff $name1 $name2 -c&quot; } else { set cmd &quot;diff $name1 $name2“ } if [catch {open &quot;|$cmd |& cat&quot;} input] { $box insert end $input } else { fileevent $input readable Log $box insert end $cmd } }
  28. 28. Example: (Con’t) # function for write a log to display area proc Log {} { global input box if [eof $input] { Stop } else { gets $input line if [regexp &quot;^<&quot; $line matches] { $box insert end $line TagA } elseif [regexp &quot;^>&quot; $line matches] { $box insert end $line TagB } else { $box insert end $line } $box see end } }
  29. 29. Example: (Con’t) # when input file is end of file proc Stop {} { global input box catch {close $input} } # clear the display area proc Clear {} { global box $box delete 1.0 end }
  30. 30. Example: Run
  31. 31. Example: Output