2016-03-28 29 views
3

我的系统,简单的OCaml图形编程'是ArchLinux的是接近它的窗口之前显示

我应该创建一个窗口,并使用图元这个简单的OCaml程序。

open Graphics 

let _ = 
    open_graph ""; 
    set_window_title "Graphics example"; 
    draw_rect 50 50 300 200; 
    set_color red; 
    fill_rect 50 50 300 200; 
    set_color blue; 
    draw_rect 100 100 200 100; 
    fill_rect 100 100 200 100 

我可以编译:

ocamlc graphics.cma -o graphics_exple graphics_exple.ml 

并与启动它:

./graphics_exple 

我在任务栏上看到一个新的窗口,采取集中再没有任何seing窗口disapear。

回答

2

这里的问题是你的程序执行一系列命令,一旦完成,它就会退出。当它退出时,它会关闭与之关联的窗口。如果您希望窗口保持打开状态,则需要防止程序退出。

一个可行的办法是在程序的末尾添加Unix.sleep 5,并用命令编译它使用Unixsleep保持窗口打开,比如说,5秒:

ocamlc graphics.cma unix.cma -o graphics_exple graphics_exple.ml 

另一种选择是通过在您最近的fill_rect之后插入一个呼叫loop()来简单地进入无限循环。凡loop的定义,像这样:最后

let rec loop() = loop() 

,你可以有一个处理程序等待来自用户的输入和对他们采取行动。为了说明方便,假设您要在控制台中打印用户键入的所有字符,但'q'会使程序退出。你只需要在你的脚本的末尾插入interactive()其中interactive被定义为:

let rec interactive() = 
    let event = wait_next_event [Key_pressed] in 
    if event.key == 'q' then exit 0 
    else print_char event.key; print_newline(); interactive() 
1

所有的桌面图形一样的Tcl/Tk或GTK有一个事件循环,必须输入给系统执行时间和显示。

你可以使用

Thread.delay 10.0 

等。

我建议像这样ocamlfind编译,与graphtemp.ml是你的测试文件:

ocamlfind ocamlc -o graphtemp -thread -package graphics -linkpkg graphtemp.ml