2015-11-19 153 views
1

我創建了一個簡單的Haskell程序顯示一個窗口:簡單的應用程序

module Main where 

import   Graphics.UI.Gtk 
import   Graphics.UI.Gtk.Glade 

data GUI = GUI { 
    mainWin :: Window, 
    clickMe :: Button, 
    display :: Label 
    } 

loadGlade :: IO GUI 
loadGlade = do 
    Just xml <- xmlNew "gladeFile.glade" 
    mw <- xmlGetWidget xml castToWindow "wdwFirst" 
    bc <- xmlGetWidget xml castToButton "btnClick" 
    ld <- xmlGetWidget xml castToLabel "lblDisplay" 

    return $ GUI mw bc ld 

connectGui :: GUI -> IO (ConnectId Button) 
connectGui gui = do 
    onDestroy (mainWin gui) mainQuit 
    onClicked (clickMe gui) (guiAnswer gui) 

guiAnswer :: GUI -> IO() 
guiAnswer gui = 
    labelSetText (display gui) "WELCOME!!" 


main :: IO() 
main = do 
    initGUI 
    gui <- loadGlade 
    connectGui gui 
    mainGUI 

與林間空地創造了這個GUI文件:

<?xml version="1.0" encoding="UTF-8"?> 
<glade-interface> 
    <!-- interface-requires gtk+ 2.24 --> 
    <!-- interface-naming-policy project-wide --> 
    <widget class="GtkWindow" id="wdwFirst"> 
    <property name="can_focus">True</property> 
    <property name="title" translatable="yes">DEBUT</property> 
    <property name="window_position">center</property> 
    <property name="default_width">400</property> 
    <property name="default_height">250</property> 
    <child> 
     <widget class="GtkLayout" id="layout1"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <child> 
      <widget class="GtkButton" id="btnClick"> 
      <property name="label" translatable="yes">Click me!</property> 
      <property name="width_request">100</property> 
      <property name="height_request">23</property> 
      <property name="visible">True</property> 
      <property name="can_focus">True</property> 
      <property name="receives_default">True</property> 
      </widget> 
      <packing> 
      <property name="x">21</property> 
      <property name="y">20</property> 
      </packing> 
     </child> 
     <child> 
      <widget class="GtkLabel" id="lblDisplay"> 
      <property name="width_request">350</property> 
      <property name="height_request">136</property> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="label" translatable="yes">gdghh 
</property> 
      </widget> 
      <packing> 
      <property name="x">43</property> 
      <property name="y">77</property> 
      </packing> 
     </child> 
     </widget> 
    </child> 
    </widget> 
</glade-interface> 

的程序編譯,並沒有運行錯誤但是......沒有出現,沒有窗口。

我用格萊德3.8.5用GTK + 2

感謝

回答

1

你忘了實際顯示窗口。函數調用如

widgetShowAll window 

似乎缺失。

編輯

這個主要功能應該工作:

main :: IO() 
    main = do 
    initGUI 
    gui <- loadGlade 
    connectGui gui 
    widgetShowAll (mainWin gui) 
    mainGUI   
+0

你是什麼意思?有沒有一個實際的函數'widgetShowAll',或者這只是一個建議? – AJFarmar

+0

有一個叫做[widgetShowAll]的函數(http://hackage.haskell.org/package/gtk-0.13.9/docs/Graphics-UI-Gtk-Abstract-Widget.html#v:widgetShowAll)。這個[最小示例](http://code.haskell.org/gtk2hs/docs/tutorial/Tutorial_Port/chap2.xhtml)顯示了您需要的功能。 (可惜這個教程已經過時了。) – lambda