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:
If it is difficult or impossible to identify code parts which will be executed frequently enough, it is also possible to use an ECLiPSe timer with set_timer/2 to produce interrupts frequently enough and to check for new events in the timer interrupt handler. This approach, however, should be used with care, because it might interfere with Xlib signal handling and is thus not quite safe.
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:
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.[eclipse 11]: tcl 'after 2000 prolog_event this event', tk_next_event(X). % waits 2 seconds X = ["this", "event"] yes.
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:
otherwise it fails:[eclipse 15]: tcl 'after 0 prolog_event early event', tk_get_event(X). X = ["early", "event"] yes.
[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.