next up previous index
Next: System Event Handling Up: Prolog as the Previous: Prolog as the

Handling Tk Events in Prolog

  When Prolog is the client, it is possible to handle some Tk events in Tcl/Tk and others in Prolog (do not confuse Prolog graphics events with the ECLiPSe event   and error handling mechanism, i.e. the predicates get_error_handler/3  , set_error_handler/2  ). Graphics events which are to be handled by Prolog will execute the command prolog_event   in the Tcl interpreter.   When

prolog_event arg1 arg2 ...
is executed in a Tk event handler, the command arguments are stored as a Prolog graphics event. This data can then be retrieved in Prolog using the predicates tk_get_event/1   and tk_next_event/1   and then a corresponding Prolog callback routine can be called. prolog_event should be executed only in a Tk event handling procedure, e.g. button or menu command or a script bound to an event using the Tk bind command. If it is executed synchronously, it will be ignored.

The structure of the Prolog program is as follows:

  1. ProTcXl is initialised, all necessary files are loaded and all graphical elements are built and displayed. Initial Prolog computation is executed.

  2. Depending on the desired functionality:

  3. When the program finishes or when it is finished by the user, Tcl/Tk is exited and the display is closed.

The predicate tk_next_event/1   waits until the next Prolog event occurs and then it returns a list of the arguments of the corresponding prolog_event command:

[eclipse 11]: tcl 'after 2000 prolog_event this event',
        tk_next_event(X).      
% waits 2 seconds
X = ["this", "event"]
yes.
Note that, as usual, the list contains strings, not atoms. Note also the use of the Tk after   command to raise an event after a specified time.

The predicate tk_get_event/1   does not wait, it returns immediately. If there is a pending Prolog graphics event in the queue, its arguments are returned in a list:

[eclipse 15]: tcl 'after 0  prolog_event early event', tk_get_event(X).

X = ["early", "event"]
yes.
otherwise it fails:
[eclipse 14]: tcl 'after 1000 prolog_event late event', tk_get_event(X).

no (more) solution.
[eclipse 15]:

Both tk_get_event/1   and tk_next_event/1   return a predefined event ["exit"] when the Tk toplevel     window is destroyed, which may happen either from the window manager or when the application exits.



next up previous index
Next: System Event Handling Up: Prolog as the Previous: Prolog as the



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