CCP4 web logo CCP4i: Graphical User Interface
Documentation for Programmers
Introduction to Tcl

next button previous button top button

Introduction to Tcl/Tk

Tcl Data Structures

Nested Commands and Some Useful Commands

Procedures

Introduction to Tcl/Tk

The interface is written in Tcl/Tk but only minimal knowledge of Tcl scripting language should be necessary to write a simple task interface. You do not need to know anything about the Tk graphics side as this is all handled by CCP4i library commands. You may find it useful to read a few chapters of one of the introductory textbooks:

Tcl and the Tk Toolkit by John Ousterhout

Addison-Wesley 1994 ISBN 0-201-63337-X

Practical Programming in Tcl and Tk by Brent Welsh

Prentice-Hall 1995 ISBN 0-13-182007-9

The latter can be found on the web:

http://www.beedub.com/book (particularly look at chapters 1 and 18)

The man pages for Tcl/Tk are also extremely useful.

Below is an introduction to some features of Tcl which are used in a task interface .tcl file.

Tcl Data Structures

Tcl treats all data as text strings except in some limited situations where it 'knows' they should be numerical. A value is assigned to a variable using the set command:

set NCYCLES 25
set MODE refinement
set TITLE "This is 25 cycles of refinement"

Here the variable NCYCLES is set to '25', MODE is set to 'refinement' and TITLE is set to 'This is 25 cycles of refinement'. Some features:

Nested Commands and Some Useful Commands

In the example above of assigning a list value:

set allowed_modes [list initialisation scaling refinement]

two Tcl commands are used: set and list. The latter is an example of a nested command which is delimited by square brackets. Everything within the square brackets is interpreted and the result substituted into the outer command. The square brackets are essential for the correct interpretation of the whole command.

Here is a very brief review of some of the commands you might see or use while working with CCP4i - see the Tcl documentation for details.

All mathematical expressions are evaluated by the expr command. I.e. to find half of the value of variable length:

set half [expr $length /2.0]

expr supports some built-in functions such as log, sqrt, sin. So to find the value of pi:

set pi [expr 2.0*asin(1.0)]

Tcl has a regular expression command regexp which is very powerful (and complex) but mostly used very simply within CCP4i. The command returns a value of 1 (true) if some part of the input string matches the test pattern, or a value of 0 (false) if it does not match. For example, to test if the value of the variable command includes the pattern 'refi':

set if_match [regexp refi $command]

There are a series of commands for handling text strings which all begin with the word string. For example, to find the length of the string in the variable 'title':

set length [string length $title]

Procedures

In Tcl a procedure (equivalent to a Fortran subroutine) is defined with the following syntax:

proc procedure_name { argument_1 ... argument_n } {
procedure body....
}

The command proc is followed by three elements: the procedure name, a list of arguments enclosed in curly braces, and the script which forms the body of the procedure which is also enclosed in curly braces. Some features of Tcl to note: