11

À few questions regarding the interactive toplevel and graphical UI programming:

  1. Is it possible to build a graphical interface dynamically from ocaml toplevel?

  2. It is possible to use the Graphics library too?

didierc
  • 14,572
  • 3
  • 32
  • 52
Fabrice Le Fessant
  • 4,222
  • 23
  • 36

2 Answers2

12
  1. You can use the LWT toplevel with integration with the lablgtk main loop:

install lwt-glib and lablgtk, on debian based systems:

apt-get install liblwt-glib-ocaml-dev liblablgtk2-ocaml-dev

in the OCaml toplevel, load everything:

#use "topfind";;
#require "lwt.simple-top";;
#require "lwt.glib";;
#require "lablgtk2";;

then initialise the GTK and the LWT integration:

GMain.init ();;
Lwt_glib.install ();;

And play:

let w = GWindow.window ();;
w#show ();;
  1. Graphics does not have a main loop like GTK, so there is no problem there. But inside a GTK application you should use cairo instead.
didierc
  • 14,572
  • 3
  • 32
  • 52
Pierre Chambart
  • 1,469
  • 12
  • 17
6

You can also use the Graphics module (see the manual). You will need to manage a lot of things yourself (the event loop, the toolkits) but it is much simpler than lablgtk:

#load "graphics.cma";;
Graphics.open_graph " ";;
Thomas
  • 5,047
  • 19
  • 30