Can't use a glade xml file with haskell

2019-03-20 02:12发布

问题:

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

Anyway, I am following the glade tutorial from the gtk2hs website. The code compiled properly 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 glade 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 my source code. The source code is an exact copy from the one in the tutorial. I have never worked with glade before, so I have no idea of whats going wrong.

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

If I switch interface with 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 change all object tags with 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 less errors by doing this but it still does not work.

回答1:

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

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

The Graphics.UI.Gtk.Glade package makes use of the libglade files, and the problem is that these are obsolete and deprecated.

At the Glade download page there are two versions available. The newer of them generates gtkbuilders meant to be used with gtk3, but since gtk2hs does not support gtk3, you'll have to download version 3.8, which generates both libglade and gtkbuilder files for gtk2.

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

I found this tutorial which makes use of gtkbuilder instead of libglade.

This program creates a window with a button, and every time you click the button you'll 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 is pulling the gui from the windowbuilder.glade file. I created that 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 glade package won't compile with ghc >= 7.6.