无论如何,我正在关注gtk2hs website的glade教程.代码编译正确,但是当我尝试执行它时,我得到了这个错误.
(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)
这是我的林间文件.
<?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>
glade文件与我的源代码位于同一目录中.源代码是教程中的完整副本.我之前从未使用过林间空地,所以我不知道出了什么问题.
我不得不将ghc从7.6降级到7.4.2.我正在使用glade 3.14.1,所有其他软件包都是通过cabal安装的,因此它们是当前版本.
如果我用glade-interface切换界面,在开始和结束时它仍然会抱怨.
(hellogtk2hs:9636): libglade-WARNING **: Unexpected element <object> inside <glade-interface>. hellogtk2hs: user error (glade.xmlGetWidget: no object named "window1" in the glade file)
如果用小部件更改所有对象标签,我会得到这个
(hellogtk2hs:9668): GLib-GObject-ERROR **: cannot create instance of abstract (non-instantiatable) type `GtkBox' `trap' para punto de parada/seguimiento
通过这样做我得到的错误更少,但它仍然不起作用.
Glade可以生成libglade文件或gtkbuilder文件.两者都是xml文件,它们之间的主要区别在于第一个以< glade-interface>开头.后来以< interface>开头.另外在gtkbuilder文件中< object>使用tag代替< widget>标签.
Graphics.UI.Gtk.Glade包使用libglade文件,问题是这些文件已过时并已弃用.
在Glade download page有两个版本可用.其中较新的生成gtkbuilders意味着与gtk3一起使用,但由于gtk2hs不支持gtk3,你必须下载3.8版本,它为gtk2生成libglade和gtkbuilder文件.
最好的办法是停止使用libglade文件并使用gtkbuilder文件.为此,您必须导入Graphics.UI.Gtk.Builder而不是Graphics.UI.Gtk.Glade
我发现this tutorial使用了gtkbuilder而不是libglade.
该程序创建一个带按钮的窗口,每次单击该按钮,您的终端上都会出现“hello world”,直到窗口关闭.
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
这是从windowbuilder.glade文件中提取gui.我使用glade-3创建了该文件.这里是.
<?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>
切换到gtkbuilder文件的另一个好理由是haskell glade包不能使用ghc> = 7.6进行编译.