next up previous index
Next: Returning Values to Up: Calling Tcl from Previous: Calling Tcl from

Passing Prolog Variables to Tcl

tcl/1   is used mainly for static commands which do not contain any Prolog variables. What happens if a part of a Tcl command is contained in a Prolog variable, e.g. the button text or its command name? One possibility would be to use the ECLiPSe predicate concat_string/2  , which takes a list of atomic terms and concatenates them all into one string:

[eclipse 12]: Text = "{Hello World}", Cmd = exit,
        concat_string(['button .b -text ', Text, ' -command ', Cmd,
        '; pack .b'], String),
        tcl(String).

Text = "{Hello World}"
Cmd = exit
String = "button .b -text {Hello World} -command exit; pack .b"
yes.
This is quite tedious, however ProTcXl provides a more user-friendly way to insert Prolog variables into Tcl scripts.   The predicate
tcl(CommandTemplate, VariableList)
first scans the string CommandTemplate. All occurrences of the substring ## are replaced by the values of subsequent variables in the list VariableList. The previous example can be thus written as
[eclipse 18]:  Text = "{Hello World}", Cmd = exit,
        tcl('button .b -text ## -command ##; pack .b', [Text, Cmd]).

Text = "{Hello World}"
Cmd = exit
yes.

As an example, we can write a predicate square(X, Y, Colour) which displays a square with the given colour at the given place in a canvas  :

square(X, Y, Colour) :-
    Size = 10,
    X1 is X + Size,
    Y1 is Y + Size,
    tcl('.c create rectangle ## ## ## ## -fill ##', [X, Y, X1, Y1, Colour]).

It can be used e.g. in the following context:

[eclipse 27]: tk([]), tcl 'canvas .c -width 100 -height 80; pack .c'.

yes.
[eclipse 28]: square(10, 20, blue), square(30, 50, yellow), square(60, 25, red).

yes.
and the three squares are displayed in the canvas:



next up previous index
Next: Returning Values to Up: Calling Tcl from Previous: Calling Tcl from



Micha Meier
Tue Jul 2 09:49:39 MET DST 1996