Glade Tutorialの例を動作させてみました。
以前うまく行かなかったのは Glade プロジェクトを開始するときに「GtkBuilder」にチェックしていたから。今回「Libglade」チェックしたところ正常に動作した。名前が「Libglade」なのだからもっと早く気付くべきでした。
hellogtk2hs.glade : Gladeの作ったファイル
<?xml version="1.0"?> <glade-interface> <!-- interface-requires gtk+ 2.16 --> <!-- interface-naming-policy project-wide --> <widget class="GtkWindow" id="window1"> <child> <widget class="GtkVBox" id="vbox1"> <property name="visible">True</property> <property name="border_width">4</property> <property name="orientation">vertical</property> <property name="spacing">4</property> <child> <widget class="GtkLabel" id="label1"> <property name="visible">True</property> <property name="label" translatable="yes">Enter your name:</property> </widget> <packing> <property name="position">0</property> </packing> </child> <child> <widget class="GtkEntry" id="entry1"> <property name="visible">True</property> <property name="can_focus">True</property> <property name="invisible_char">●</property> </widget> <packing> <property name="position">1</property> </packing> </child> <child> <widget class="GtkHBox" id="hbox1"> <property name="visible">True</property> <property name="border_width">4</property> <property name="spacing">3</property> <property name="homogeneous">True</property> <child> <widget 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> </widget> <packing> <property name="position">0</property> </packing> </child> <child> <widget 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> </widget> <packing> <property name="position">1</property> </packing> </child> </widget> <packing> <property name="position">2</property> </packing> </child> </widget> </child> </widget> </glade-interface>
module Main where import Graphics.UI.Gtk import Graphics.UI.Gtk.Glade main = do initGUI Just xml <- xmlNew "hellogtk2hs.glade" window <- xmlGetWidget xml castToWindow "window1" onDestroy window mainQuit closeButton <- xmlGetWidget xml castToButton "button2" onClicked closeButton $ do widgetDestroy window label <- xmlGetWidget xml castToLabel "label1" entry <- xmlGetWidget xml castToEntry "entry1" applyButton <- xmlGetWidget xml castToButton "button1" onClicked applyButton $ do name <- get entry entryText set label [ labelText := "Hello " ++ name ] widgetShowAll window mainGUI
> ghc --make -optl-mwindows helloGtk2hs.hs -o hellogtk2hs [1 of 1] Compiling Main ( helloGtk2hs.hs, helloGtk2hs.o ) Linking hellogtk2hs.exe ... > ./hellogtk2hs