2012-11-08 77 views
7

我不確定我是否應該打招呼或者不打招呼,因爲這是我的第一篇文章。haskell不能使用glade xml文件

無論如何,我正在關注來自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安裝的,因此它們都是當前版本。

如果我切換接口空地接口,在開始和它仍然抱怨結束。

(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 

我做這個得到更少的錯誤,但它仍然無法正常工作。

+0

你有什麼版本的各種組件? – huon

+1

我想問題在這裏:'<! - interface-requires gtk + 3.0 - >'; gtk2hs是gtk2綁定。 –

+0

(如果你找到了解決方案,你應該把它作爲答案並接受它,這樣問題就不會被解決,或者@MatveyAksenov,你可以將你的評論作爲答案。) – huon

回答

19

問題是gtk2hs不支持gtk3,我創建的文件是gtk3。

Glade可以生成libglade文件或gtkbuilder文件。兩者都是xml文件,它們之間的主要區別在於第一個以<glade-interface>開頭,後面以<interface>開頭。同樣在gtkbuilder文件中,使用<object>標籤代替<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的空地包將不會與GHC> = 7.6編譯。

+1

很好的回答;感謝那。 – AndrewC

相關問題