haskell不能使用glade xml文件 [英] Can't use a glade xml file with haskell

查看:225
本文介绍了haskell不能使用glade xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



无论如何,我正在关注来自的glade教程。

我不确定是否应该打招呼或者不打招呼。 href =http://projects.haskell.org/gtk2hs/docs/tutorial/glade/> gtk2hs网站。代码编译正确,但是当我尝试执行它时,我得到这个错误。

 (hellogtk2hs:8670):libglade-WARNING **:预计< glade-interface> ;. Got< interface> ;. 
(hellogtk2hs:8670):libglade-WARNING **:未完成PARSER_FINISH状态
hellogtk2hs:用户错误(模式匹配失败,HelloGtk2Hs.hs中的表达式为:8:8-15)

这是我的glade文件。

 <?xml version =1.0encoding =UTF-8?> 
< interface>
<! - interface-requires gtk + 3.0 - >
< object class =GtkWindowid =window1>
< property name =can_focus> False< / property>
< child>
< object class =GtkBoxid =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 =GtkLabelid =label1>
< property name =visible> True< / property>
< property name =can_focus> False< / property>
< property name =labeltranslatable =yes>在这里写下您的名字:< / property>
< / object>
<包装>
< property name =expand> False< / property>
< property name =fill> True< / property>
< property name =position> 0< / property>
< / packing>
< / child>
< child>
< object class =GtkEntryid =entry1>
< property name =visible> True< / property>
< property name =can_focus> True< / property>
< property name =invisible_char>●< / property>
< / object>
<包装>
< property name =expand> False< / property>
< property name =fill> True< / property>
< property name =position> 1< / property>
< / packing>
< / child>
< child>
< object class =GtkBoxid =box2>
< property name =visible> True< / property>
< property name =can_focus> False< / property>
< property name =homogeneous> True< / property>
< child>
< object class =GtkButtonid =button1>
< property name =label> gtk-apply< / property>
< property name =visible> True< / property>
< property name =can_focus> True< / property>
< property name =receiving_default> True< / property>
< property name =use_stock> True< / property>
< property name =always_show_image> True< / property>
< / object>
<包装>
< property name =expand> False< / property>
< property name =fill> True< / property>
< property name =position> 0< / property>
< / packing>
< / child>
< child>
< object class =GtkButtonid =button2>
< property name =label> gtk-close< / property>
< property name =visible> True< / property>
< property name =can_focus> True< / property>
< property name =receiving_default> True< / property>
< property name =use_stock> True< / property>
< property name =always_show_image> True< / property>
< / object>
<包装>
< property name =expand> False< / property>
< property name =fill> True< / property>
< property name =position> 1< / property>
< / packing>
< / child>
< / object>
<包装>
< property name =expand> False< / property>
< property name =fill> True< / property>
< property name =position> 2< / property>
< / packing>
< / child>
< / object>
< / child>
< / object>
< / interface>

glade文件与我的源代码位于同一个目录中。源代码与本教程中的源代码完全相同。我之前从未和glade一起工作过,所以我不知道发生了什么问题。



我必须将ghc从7.6降级到7.4.2。我正在使用glade 3.14.1,所有其他软件包都是通过cabal安装的,因此它们都是当前版本。



如果我使用 glade-interface 切换 interface ,那么在开始和结束时它仍然会抱怨。 / p>

 (hellogtk2hs:9636):libglade-WARNING **:意外的元素< object>在< glade-interface>内部。 
hellogtk2hs:用户错误(glade.xmlGetWidget:glade文件中没有名为window1的对象)

如果用 widget修改所有 object 标签,我就会得到这个

 (hellogtk2hs:9668):GLib-GObject-ERROR **:无法创建抽象(非实例化)类型`GtkBox'类型的实例
`trap'para $ p $ b $ / $ se $ s

通过这样做可以减少错误,但它仍然无效。

解决方案

问题是,gtk2hs不支持gtk3,我创建的文件是gtk3。



Glade可以生成一个libglade文件或gtkbuilder文件。两者都是xml文件,它们之间的主要区别在于第一个以< glade-interface> 开头,后者以< interface>开始; 。此外,在gtkbuilder文件中,使用< object> 标记代替< widget> 标记。



Graphics.UI.Gtk.Glade 包使用libglade文件,问题是这些文件已经过时,已弃用。



Glade下载页面有两个版本可用。他们中较新的人生成了gtkbuilders,意在与gtk3一起使用,但由于gtk2hs不支持gtk3,因此您必须下载3.8版本,该版本同时为gtk2生成libglade和gtkbuilder文件。

最好的办法是停止使用libglade文件并改用gtkbuilder文件。为此,您必须导入 Graphics.UI.Gtk.Builder 而不是 Graphics.UI.Gtk.Glade



我发现本教程使用gtkbuilder代替libglade。



这个程序创建一个带有按钮的窗口,每次点击您将在终端上获得hello world按钮,直到窗口关闭。

  import Graphics.UI.Gtk 
import Graphics.UI.Gtk.Builder
$ b $ main = do
initGUI
builder< - builderNew
builderAddFromFile builderwindowbuilder.glade
mainWindow< - builderGetObject builder castToWindowmain_window
onDestroy mainWindow mainQuit
helloWorldButton< - builderGetObject builder castToButtonhello_world_button
onClicked helloWorldButton(putStrLnHello,World!)
widge tShowAll mainWindow
mainGUI

这是从windowbuilder.glade文件中拉出gui。我使用glade-3创建了该文件。

 <?xml version =1.0encoding =UTF-8?> 
< interface>
< requires lib =gtk +version =2.24/>
<! - interface-naming-policy project-wide - >
< object class =GtkWindowid =main_window>
< property name =width_request> 210< / property>
< property name =can_focus> False< / property>
< property name =titletranslatable =yes>这是一个窗口< / property>
< property name =resizable> False< / property>
< child>
< object class =GtkVBoxid =vbox1>
< property name =visible> True< / property>
< property name =can_focus> False< / property>
< child>
< object class =GtkLabelid =label1>
< property name =visible> True< / property>
< property name =can_focus> False< / property>
< property name =labeltranslatable =yes>点击按钮!!< / property>
< / object>
<包装>
< property name =expand> True< / property>
< property name =fill> True< / property>
< property name =position> 0< / property>
< / packing>
< / child>
< child>
< object class =GtkButtonid =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 =receiving_default> True< / property>
< property name =border_width> 7< / property>
< property name =use_stock> True< / property>
< / object>
<包装>
< 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>

切换到gtkbuilder文件的另一个很好的理由是haskell glade软件包不会使用ghc> = 7.6。

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.

解决方案

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.

这篇关于haskell不能使用glade xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆