The tcltk
library package is a bidirectional interface to the
Tcl (pronounced Tickle) language and the Tk toolkit. Tcl is
an interpreted scripting language with many extension packages, in
particular the graphical interface toolkit Tk.
You can read about Tcl/Tk in [Ousterhout 94] or in various articles. The articles and the Tcl/Tk system can be found by anonymous FTP, see the Release Notes for up-to-date information. This file also contains information on how to include Tcl/Tk extensions.
To load the package, enter the query:
| ?- use_module(library(tcltk)).
To use Tcl, you must create a Tcl interpreter object and send commands to it. The following predicate executes a single Tcl command:
test_command(Command, Result) :- tcl_new(Interp), tcl_eval(Interp, Command, Result), tcl_delete(Interp).
The Tcl command and its arguments is specified in Command (see section Command Format), and the result will be returned as a string (list of character codes) in Result.
These are the predicates to use Tcl/Tk from Prolog:
tcl_new(-TclInterpreter)
TCL_LIBRARY
.
tcl_delete(+TclInterpreter)
tcl_eval(+TclInterpreter, +Command, -Result)
tcl_event(+TclInterpreter, +Command, -Events)
tcl_eval/3
. Events is a list of
terms stored from Tcl by the prolog_event
command (see section Tcl to Prolog).
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 -->[]
|[ListExpr]
ListExpr --> Command |Command,ListExpr
|Command|ListOfCommands
where
Atom
Number
chars(PrologString)
write(Term)
format(Fmt,Args)
dq(Command)
br(Command)
sqb(Command)
min(Command)
ListOfCommands
Examples of command specifications and corresponding translations:
'set x 32' ==> set x 32 [set,x,br([a,b,c])] ==> set x {a b c} ['.panel.value_info.name',configure,min(text),br(write('$display'/1))] ==> .panel.value_info.name configure -text {$display/1}
There are two new Tcl commands defined in this library:
prolog Goal
user
module unless it is
prefixed with another module name. The call is always deterministic.
The result of the call is returned as:
"1"
prolog_variables
with the variable name as index.
The term is converted to Tcl using the same conversion as used for Tcl
commands (see section Command Format).
"0"
Tcl exception
Prolog
Exception:
" appended with a string representation of the Prolog
exception.
test_callback(Result) :- tcl_new(Interp), tcl_eval(Interp, 'if {[prolog "foo(X,Y,Z)"] == 1} \\ {lappend res $prolog_variables(X) \\ $prolog_variables(Y) \\ $prolog_variables(Z)}', Result), tcl_delete(Interp). foo(1, bar, [a,b,c]).The query
| ?- test_callback(Result).will succeed, binding Result to:
"1 bar {a b c}"
prolog_event Terms...
tcl_event/3
or tk_next_event/(2,3)
.
An example:
test_event(Event) :- tcl_new(Interp), tcl_event(Interp, [prolog_event,dq(write(zap(42)))], Event), tcl_delete(Interp).
The query
| ?- test_event(Event).
will succeed, binding Event to the list [zap(42)]
The following example is a simple "Hello, world" program:
tk_hello_world :- tk_new([], Interp), tcl_eval(Interp, 'button .b -text "Hello" -command "destroy ."', _), tcl_eval(Interp, 'pack .b', _), tk_main_loop, tcl_delete(Interp).
The program brings up a tiny window with a button labeled "Hello" and is terminated by pressing the button.
These are the predicates to use Tk from Prolog:
tk_new(+Options, -TclInterpreter)
TK_LIBRARY
. Options is a list of
optional elements according to:
top_level_events
name(+ApplicationName)
display(+Display)
DISPLAY
environment variable.
tk_main_window(+TclInterpreter, -TkWindow)
tk_make_window_exist(+TkWindow)
tk_destroy_window(+TkWindow)
tk_num_main_windows(-NumberOfWindows)
tk_do_one_event
tk_do_one_event(+ListOrBitmask)
tk_dont_wait
tk_x_events
tk_window_events
tk_file_events
tk_timer_events
tk_idle_events
tk_all_events
tk_do_one_event/0
is equivalent to tk_do_one_event/1
with
all flags set. If the tk_dont_wait
flag is set and there is no
event to handle, the call will fail.
It is straight-forward to define a predicate which handles
all events in the queue and then returns:
tk_do_all_events :- tk_do_one_event, !, tk_do_all_events. tk_do_all_events.
tk_do_one_event/(0,1)
is an interface to the C-function
TkDoOneEvent()
(or TclDoOneEvent()
in later Tcl/Tk
versions).
tk_next_event(+TclInterpreter, -Event)
tk_next_event(+ListOrBitmask, +TclInterpreter, -Event)
prolog_event
command (see section Tcl to Prolog). Only
events associated with TclInterpreter are returned; other events
remain in the queue. If there are no windows left the term []
will be returned. This predicate does not correspond directly to any
Tcl/Tk C function.
tk_main_loop
There are basically two alternatives for invoking Prolog actions on user
events. The first is to directly call Prolog by means of the Tcl command
prolog
. Tcl/Tk must then be repeatedly invoked, either by calling
tk_main_loop/0
or using the option top_level_events
,
possibly in conjunction with calling tk_do_one_event/(0,1)
in
lengthy computations.
The second alternative is using the Tcl command prolog_event
. The
user program then, by calling tk_next_event/(2,3)
, passes control
to Tcl/Tk until one or more invocations of prolog_event
has
occurred. The Prolog event determines the Prolog action to take. This
approach has the advantage that context variables might be passed around
in the event loop.
Go to the first, previous, next, last section, table of contents.