The Tk command after sets up a timer which raises a Tk event after the specified time elapses and executes the given command script. This feature can be used to write specialised commands. For instance, we can define a predicate tk_sleep(N) which sleeps for the specified number of seconds, but unlike the ECLiPSe sleep/1 predicate, it processes Tk events in the meantime. We simply block in tk_next_event/1 , handle all events and as soon as the timeout event occurs, we return:
We create a button which sends a prolog event. Now we can see that if we call tk_sleep/1, the button is active, its event is served, but the execution blocks as required:tk_sleep(N) :- MS is fix(N*1000), % seconds to milliseconds tcl('after ## prolog_event timeout', [MS]), handle_events. handle_events :- tk_next_event([E|L]), handle_event(E, L). handle_event("timeout", []) :- !. % we can return from the predicate handle_event("exit", []) :- !. handle_event("button", []) :- writeln('button pressed'), handle_events. % for other events we loop
[eclipse 9]: tk([]), tcl 'button .b -text Button -command {prolog_event button}', tcl 'pack .b'. yes. [eclipse 10]: tk_sleep(10). button pressed button pressed yes.