4

I am working on a program in which I would like to use lablgtk and lwt. I have functions fetching data on lwt threads, then I would like to display the data in a GUI using lablgtk. I am struggling with the integration of lablgtk in the lwt framework. Below is a distilled version that demonstrates my problem which is that I can get the GUI to launch, but then nothing else happens. Any help is greatly appreciated.

(* gtk_and_lwt.ml *)

let (>>=) = Lwt.bind

let locale = GtkMain.Main.init ()

let window = GWindow.window ~width:300 ~height:200 ()

let vbox = GPack.vbox ~border_width:2 ~packing:window#add ()

let my_table =
  GPack.table
    ~rows:1
    ~columns:2
    ~row_spacings:5
    ~col_spacings:5
    ~border_width:5
    ~packing:vbox#pack
    ()

let _ =
  GMisc.label
    ~text:"User Input"
    ~packing:(my_table#attach ~left:0 ~top:1 ~right:1 ~bottom:2)
    ()

let my_label =
  GMisc.label
    ~text:"Fetching User Input..."
    ~packing:(my_table#attach ~left:1 ~top:0 ~right:2 ~bottom:1)
    ()

let rec get_user_input () =
  Lwt_io.read_line Lwt_io.stdin
  >>= fun s -> my_label#set_text s |> Lwt.return
  >>= get_user_input

let main () =

  (* Kill the process when the user clicks the "X" button in top left cornet*)
  let _ = window#connect#destroy ~callback:GMain.Main.quit in

  window#show ();
  Lwt.async (get_user_input);
  Lwt.return @@ GMain.Main.main ()

let _ = Lwt_main.run @@ main ()

I am compiling with ocamlfind ocamlc -g -package lwt,lwt.syntax,lwt.unix,lablgtk2 -linkpkg gtk_and_lwt.ml -o GtkAndLwt

Thomas
  • 347
  • 3
  • 19
  • 2
    I haven't used Lwt and lablgtk together before but I think you'll want to use this module: https://ocsigen.org/lwt/2.5.1/api/Lwt_glib – hcarty Jun 05 '16 at 12:05
  • 3
    That worked great, don't know how I never saw that page before. All I had to do was add Lwt_glib.installl, remove GMain.Main.main and return a thread created using Lwt.wait. – Thomas Jun 05 '16 at 14:47
  • Good! I'm glad it worked. – hcarty Jun 05 '16 at 20:57

0 Answers0