Author(s): Montse Iglesias Urraca, http://www.clip.dia.fi.upm.es/
, The CLIP Group, Facultad de Informática, Universidad Politécnica de Madrid.
Version: 1.10#7 (2006/4/26, 19:22:13 CEST)
Version of last change: 1.9#314 (2004/2/25, 18:27:47 CET)
The
tcltk
library package is a bidirectional interface to the Tcl language and the Tk toolkit. Tcl is an interpreted scripting language with many extension packages, particularly the graphical interface toolkit, Tk. The interaction between both languages is expressed in terms of an interface between the Tcl/Tk process and the Prolog process. This approach allows the development of mixed applications where both sides, Tcl/Tk and Prolog, can be combined in order to exploit their respective capabilities.
This library uses two sockets to connect both the Tcl and the Prolog processes: event_socket and term_socket. There are also two Tcl global variables: prolog_variables and terms. The value of any of the bound variables in a goal will be stored in the array prolog_variables
with the variable name as index. Terms is the string which contains the printed representation of prolog terms.
Prolog to Tcl
The Tcl/Tk side waits for requests from the Prolog side, and executes the Tcl/Tk code received. Also, the Tcl/Tk side handles the events and exceptions which may be raised on its side, passing on control to the Prolog side in case it is necessary.
To use Tcl, you must create a Tcl interpreter object and send commands to it. A Tcl command is specified as follows:
Command --> Atom { other than [] } | Number | chars(PrologString) | write(Term) | format(Fmt,Args) | dq(Command) | br(Command) | sqb(Command) | min(Command) | ListOfCommands ListOfCommands --> [] |[Command|ListOfCommands]
where:
Atom
Number
chars(PrologString)
write(Term)
format(Term)
dq(Command)
br(Command)
sqb(Command)
min(Command)
ListOfCommands
The predicates to use Tcl from Prolog are
tcl_new/1
,
tcl_delete/1
,
tcl_eval/3
, and
tcl_event/3
.
An example of use with Prolog as master and Tcl as slave, consisting of a GUI to a program which calculates the factorial of a number:
:- use_module(library(tcltk)). go :- tcl_new(X), tcl_eval(X,[button,'.b',min(text),dq('Compute!')],_), tcl_eval(X,[button,'.c','-text',dq('Quit')],_), tcl_eval(X,[entry,'.e1',min(textvariable),'inputval'],_), tcl_eval(X,[label,'.l1',min(text),dq('The factorial of ')],_), tcl_eval(X,[pack, '.l1','.e1'],_), tcl_eval(X,[entry,'.e2',min(textvariable),'outputval'],_), tcl_eval(X,[label,'.l2',min(text),dq('is ')],_), tcl_eval(X,[pack, '.l2','.e2'],_), tcl_eval(X,[pack,'.b','.c',min(side),'left'],_), tcl_eval(X,[bind,'.b','<ButtonPress-1>', br([set,'inputval','$inputval','\n', prolog_one_event,dq(write(execute(tk_test_aux:factorial('$inputval','Outputval')))),'\n', set, 'outputval','$prolog_variables(Outputval)'])],_), tcl_eval(X,[bind,'.c','<ButtonPress-1>', br([prolog_one_event,dq(write(execute(exit_tk_event_loop)))])],_), tk_event_loop(X).
Tcl to Prolog
This is the usual way to build a GUI application. The slave, Prolog, behaves as a server that fulfills eventual requests from the master side, Tcl. At some point, during the user interaction with the GUI, an action may take place that triggers the execution of some procedure on the slave side (a form submit, for example). Thus, the slave is invoked, performs a service, and returns the result to the GUI through the socket connection.
This library includes two main specific Tcl commands:
prolog
Goal
prolog_event
Term
tcl_event/3
and
tk_next_event/2
.
Additionally, seven extra Tcl commands are defined.
prolog_delete_event
prolog_list_events
prolog_cmd
Command
prolog_one_event
Term
prolog_thread_event
Term
convert_variables
String
unify_term
Term1 Term2
The predicates to use Prolog from Tcl are
tk_event_loop/1
,
tk_main_loop/1
,
tk_new/2
, and
tk_next_event/2
.
An example of use with Tcl as master and Prolog as slave, implementing the well known "Hello, world!" dummy program (more can be seen in directory examples):
Prolog side:
:- use_module(library(tcltk)). :- use_package(classic). hello('Hello, world!'). go :- tk_new([name('Simple')], Tcl), tcl_eval(Tcl, 'source simple.tcl', _), tk_main_loop(Tcl), tcl_delete(Tcl).
Tcl side (simple.tcl):
label .l -textvariable tvar button .b -text "Go!" -command {run} pack .l .b -side top proc run {} { global prolog_variables global tvar prolog hello(X) set tvar $prolog_variables(X) }
tcltk
):- use_module(library(tcltk)).
tcltk
)
Usage: tcl_new(-TclInterpreter)
TclInterpreter
.
-TclInterpreter
is a reference to a Tcl interpreter.
(tcltk:tclInterpreter/1
)
Meta-predicate with arguments: tcl_eval(?,?,addmodule)
.
Usage: tcl_eval(+TclInterpreter, +Command, -Result)
Command
in the Tcl interpreter TclInterpreter
. The result will be stored as a string in Result
. If there is an error in Command an exception is raised. The error messages will be Tcl Exception: if the error is in the syntax of the Tcl/Tk code or Prolog Exception:, if the error is in the prolog term.
+TclInterpreter
is a reference to a Tcl interpreter.
(tcltk:tclInterpreter/1
)
+Command
is a Tcl command.
(tcltk:tclCommand/1
)
-Result
is a string (a list of character codes).
(basic_props:string/1
)
Usage: tcl_delete(+TclInterpreter)
TclInterpreter
, it deletes the interpreter from the system.
+TclInterpreter
is a reference to a Tcl interpreter.
(tcltk:tclInterpreter/1
)
Usage: tcl_event(+TclInterpreter, +Command, -Events)
Command
in the Tcl interpreter whose handle is provided in TclInterpreter
. Events
is a list of terms stored from Tcl by prolog_event. Blocks until there is something on the event queue
+TclInterpreter
is a reference to a Tcl interpreter.
(tcltk:tclInterpreter/1
)
+Command
is a Tcl command.
(tcltk:tclCommand/1
)
-Events
is a list.
(basic_props:list/1
)
Usage: tclInterpreter(I)
I
is a reference to a Tcl interpreter.
Usage: tclCommand(C)
C
is a Tcl command.
Usage: tk_event_loop(+TclInterpreter)
execute(Goal)
, the predicate silently exits. If the execution of Goal
raises a Prolog error, the interpreter is deleted and an error message is given.
+TclInterpreter
is a reference to a Tcl interpreter.
(tcltk:tclInterpreter/1
)
Usage: tk_main_loop(+TclInterpreter)
+TclInterpreter
is a reference to a Tcl interpreter.
(tcltk:tclInterpreter/1
)
Usage: tk_new(+Options, -TclInterpreter)
Options
is a list of optional elements according to:
name(+ApplicationName)
ApplicationName
. It is also used for communicating between Tcl/Tk applications via the Tcl send command. Default name is an empty string.
display(+Display)
DISPLAY
environment variable.
file
file
. Commands will not be read from standard input and the execution returns back to Prolog only after all windows (and the interpreter) have been deleted.
+Options
is a list.
(basic_props:list/1
)
-TclInterpreter
is a reference to a Tcl interpreter.
(tcltk:tclInterpreter/1
)
Usage: tk_next_event(+TclInterpreter, -Event)
TclInterpreter
. Event
is the term correspondig to the head of a queue of events stored from Tcl with the prolog_event command.
+TclInterpreter
is a reference to a Tcl interpreter.
(tcltk:tclInterpreter/1
)
-Event
is a string (a list of character codes).
(basic_props:string/1
)
Go to the first, previous, next, last section, table of contents.