Tcl/Tk Cookbook - Using Extensions


Step 1: Script

Create a script file bltex.tcl in your current working directory for this example. The script is interlaced with description text in italics.


blt_graph .g
.g config -title {Do they meet?}
pack .g -padx 5 -pady 5

create and pack an instance ".g" of the blt_graph with title "Do they meet?"



set x {.1 .2 .3 .4 .5 .6 .7 .8 .9}
set y {}
foreach i $x {
   set yt [expr pow($i,2)*(1-$i) ]
   lappend y $yt
}
.g element create curve1 -symbol diamond -x $x -y $y 


The variable x is set to a list of values ranging from 0.1 to 0.9 and the variable y is initialised to a null list.

For each value i of x, yt is calculated as a simple arithmetic expression. Note that the Tcl expr command and the Tcl math expression pow are used. The computed value of yt is appended to y using the Tcl list command lappend.

The last line above invokes the widget command .g for creating an element which is referred to as curve1. The options -x and -y specify the list of (x,y) points on the curve.

The "-symbol" option takes the value diamond and will highlight the nodes of curve1 with diamond shaped markers.



set y2 {}
foreach i $x {
    set yt [expr pow($i,2)]
    lappend y2 $yt
}
.g element create curve2 -symbol cross -x $x -y $y2

The script above creates a second curve curve2, with nodes highlighted by a cross marker.


button .quit -text Quit -command exit
pack .quit -fill x

Place a button to invoke the exit command.

Make this file bltex.tcl executable. If you have a super Tcl that includes blt (see chapter 9), you can invoke it in interactive mode by typing stcl at the commandline (ensure you give the correct pathname to stcl). Alternatively you can invoke blt_wish.

At the stcl or blt_wish prompt type source bltex.tcl. The result will be: