Node:hello world, Next:, Previous:A quick example of Tcl/Tk in action, Up:A quick example of Tcl/Tk in action



hello world

Here is the program; also in library('tcltk/examples/ex1.pl'):

:- use_module(library(tcltk)).

go :-
    tk_new([name('Example 1')], Interp),
    tcl_eval(Interp, 'button .fred -text "hello world" -command { puts "hello world"}', _),
    tcl_eval(Interp, 'pack .fred', _),
    tk_main_loop.

images/tcltkex1.png

SICStus+Tcl/Tk hello world program.
To run it just start up SICStus (on Windows use sicstus, not spwin), load the program, and evaluate the Prolog goal go. The first line of the go clause calls tk_new/2 which creates a Tcl/Tk interpreter and returns a handle Interp through which Prolog will interact with the interpreter. Next a call to tcl_eval/3 is made which creates a button displaying the hello world text. Next a call is made to tcl_eval/3 that causes the button to be displayed in the main application window. Finally, a call is make to tk_main_loop/0 that passes control to Tcl/Tk, making sure that window events are serviced.

See how simple it is with just a three line Prolog program to create an application window and display a button in it. Click on the button and see what it does.

The reason you should use sicstus on Windows instead of spwin is that the latter does not have the C standard streams (stdin,stdout,stderr) and the Tcl command puts will give an error if there is no stdout.