Tcl/Tk Pitfalls

www.breakup.de

Content

Frames and objects

A feature of the graphical packer pack is something strange :
To pack objects (e.g. entry, button) into a frame, the objects has to be created after the frame. If not the object wouldn't be shown.
The effect can be evaluated with the following tcl script. It produces the output on the right side. The label .w is created and packed, but not shown by pack.
No old objects into new frames.

label .w -text "World!" -bg red
frame .hw
label .h -text Hello

pack .w .h -in .hw -side bottom
pack .hw

button .exit -text Exit -command { exit }
pack .exit

# make an object (this is the lost one)
# make the frame
# make another object (to show that pack will do something)

# pack all objects into the frame
# pack the frame to the window

# make an exit button
# and bring it to the window

 

Mathematical accuracy

For mathematical calculations beside basic arithmetics don't forget to set mathematical precission by


set tcl_precission 17

If you donīt do it, you will lose significant digits.

Here is a calculation with normal precission for the energy of a proton depending on it's velocity. On the graphs below the same calculations with a Tcl script and a C program are shown. The output of the Tcl calculation (red) has an error (1%) which can't be used in computational calculations.

The C program that makes one side of the calculations and also writes and starts execution of the Tcl side is available for UNIX. The program executes the Tcl script with a tclsh. For the creation of the following graph there was a shell script used. The script executes gcc for making the C program and gnuplot for plotting the graph.

Mathematiclal inaccuracy of Tcl

The problem in this calculation is the usage of the square root function sqrt (see following graph).

Difference of C and Tcl calculations

Usage of multiple Tcl/Tk versions

If more than one version of Tcl/Tk is used, some files have to be managed. For Tcl there are the following files for each version :

File Path Description
 tclsh /usr/local/bin Tcl-shell
 tclConfig.sh /usr/local/lib shell-script with environment for Tcl
 tcl.h /usr/local/include header file
 libtcl.a /usr/local/lib static library for Tcl
 libtcl.so /usr/local/lib shared library for Tcl

If there are only one version is used, there is no need for storing Tcl in the local/ path. The graphical toolkit Tk is located in the path of the X Window system. For Tk there are the following files used for each version :

File Path Description
 wish /usr/X11R6/bin windowing shell for Tcl/Tk
 tkConfig.sh /usr/X11R6/lib shell-script with environment for Tk
 tk.h /usr/X11R6/include header file
 libtk.a /usr/X11R6/lib static library for Tk
 libtk.so /usr/X11R6/lib shared library for Tk

To handle the different versions of Tcl/Tk I suggest to rename the files above in the following way :

Names by version,   <VER> = <MAJOR_VERSION>.<MINOR_VERSION>
tclsh<VER> tclConfig<VER>.sh tcl<VER>.h libtcl<VER>.a libtcl<VER>.so
wish<VER> tkConfig<VER>.sh tk<VER>.h libtk<VER>.a libtk<VER>.so
Example :
tclsh7.6 tclConfig7.6.sh tcl7.6.h libtcl7.6.a libtcl7.6.so
wish4.2 tkConfig4.2.sh tk4.2.h libtk4.2.a libtk4.2.so

For access to the highest or general used version, make symbolic links to this version. While renaming the files, don't forget to copy the changes into the tclConfig.sh and tkConfig.sh files.

©1999 Martin Hachenberg Return to Computer-Page