Cannot use glade xml file with haskell

I'm not sure if I should say hi or not, being my first post here.

In any case, I am following the glade tutorial from the gtk2hs website. The code compiled correctly, but when I try to execute it, I get this error.

(hellogtk2hs:8670): libglade-WARNING **: Expected <glade-interface>. Got <interface>. (hellogtk2hs:8670): libglade-WARNING **: did not finish in PARSER_FINISH state hellogtk2hs: user error (Pattern match failure in do expression at HelloGtk2Hs.hs:8:8-15) 

This is my field file.

 <?xml version="1.0" encoding="UTF-8"?> <interface> <!-- interface-requires gtk+ 3.0 --> <object class="GtkWindow" id="window1"> <property name="can_focus">False</property> <child> <object class="GtkBox" id="box1"> <property name="visible">True</property> <property name="can_focus">False</property> <property name="orientation">vertical</property> <property name="homogeneous">True</property> <child> <object class="GtkLabel" id="label1"> <property name="visible">True</property> <property name="can_focus">False</property> <property name="label" translatable="yes">Write your name here: </property> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">0</property> </packing> </child> <child> <object class="GtkEntry" id="entry1"> <property name="visible">True</property> <property name="can_focus">True</property> <property name="invisible_char">●</property> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">1</property> </packing> </child> <child> <object class="GtkBox" id="box2"> <property name="visible">True</property> <property name="can_focus">False</property> <property name="homogeneous">True</property> <child> <object class="GtkButton" id="button1"> <property name="label">gtk-apply</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <property name="use_stock">True</property> <property name="always_show_image">True</property> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">0</property> </packing> </child> <child> <object class="GtkButton" id="button2"> <property name="label">gtk-close</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <property name="use_stock">True</property> <property name="always_show_image">True</property> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">1</property> </packing> </child> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">2</property> </packing> </child> </object> </child> </object> </interface> 

The glade file is in the same directory as the source code. The source code is an exact copy of the one in the textbook. I had never worked with a clearing before, so I have no idea what is going wrong.

I had to downgrade ghc from 7.6 to 7.4.2. I use glade 3.14.1, all other packages were installed through cabal, so they are on their current version.

If I switch the interface using the glade interface, at the beginning and at the end it still complains.

 (hellogtk2hs:9636): libglade-WARNING **: Unexpected element <object> inside <glade-interface>. hellogtk2hs: user error (glade.xmlGetWidget: no object named "window1" in the glade file) 

And if I change all the object tags using the widget, I get this

 (hellogtk2hs:9668): GLib-GObject-ERROR **: cannot create instance of abstract (non-instantiatable) type `GtkBox' `trap' para punto de parada/seguimiento 

I get fewer errors doing this, but it still does not work.

+7
source share
1 answer

The problem was that gtk2hs does not support gtk3, and the file I created was for gtk3.

Glade can generate a libglade file or a gtkbuilder file. Both are xml files, and the main difference between them is that the first starts with <glade-interface> , and later starts with <interface> . Gtkbuilder files also use the <object> tag instead of the <widget> tag.

The Graphics.UI.Gtk.Glade package uses libglade files, and the problem is that they are outdated and outdated.

Two versions are available on the Glade download page. The newer one is generated by gtkbuilders, designed for use with gtk3, but since gtk2hs does not support gtk3, you will have to download version 3.8, which generates the libglade and gtkbuilder files for gtk2.

It is best to stop using libglade files and use gtkbuilder files instead. To do this, you must import Graphics.UI.Gtk.Builder instead of Graphics.UI.Gtk.Glade

I found this tutorial that uses gtkbuilder instead of libglade.

This program creates a window with a button, and each time you press the button, you get a "hello world" on your terminal until the window is closed.

 import Graphics.UI.Gtk import Graphics.UI.Gtk.Builder main = do initGUI builder <- builderNew builderAddFromFile builder "windowbuilder.glade" mainWindow <- builderGetObject builder castToWindow "main_window" onDestroy mainWindow mainQuit helloWorldButton <- builderGetObject builder castToButton "hello_world_button" onClicked helloWorldButton (putStrLn "Hello, World!") widgetShowAll mainWindow mainGUI 

This pulls gui from the windowbuilder.glade file. I created this file using glade-3. Here it is.

 <?xml version="1.0" encoding="UTF-8"?> <interface> <requires lib="gtk+" version="2.24"/> <!-- interface-naming-policy project-wide --> <object class="GtkWindow" id="main_window"> <property name="width_request">210</property> <property name="can_focus">False</property> <property name="title" translatable="yes">This is a window</property> <property name="resizable">False</property> <child> <object class="GtkVBox" id="vbox1"> <property name="visible">True</property> <property name="can_focus">False</property> <child> <object class="GtkLabel" id="label1"> <property name="visible">True</property> <property name="can_focus">False</property> <property name="label" translatable="yes">Click the button!!</property> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">0</property> </packing> </child> <child> <object class="GtkButton" id="hello_world_button"> <property name="label">gtk-ok</property> <property name="use_action_appearance">False</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <property name="border_width">7</property> <property name="use_stock">True</property> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="padding">5</property> <property name="position">1</property> </packing> </child> </object> </child> </object> </interface> 

Another good reason to switch to gtkbuilder files is that the haskell firmware package will not compile with ghc> = 7.6.

+19
source

All Articles