在Haskell中用于gui编程的简单应用程序

时间:2015-11-19 20:54:57

标签: user-interface haskell

我创建了一个简单的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

使用glade创建的这个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>

程序编译,运行没有错误,但......没有出现,没有窗口。

我使用glade 3.8.5和gtk + 2

感谢

1 个答案:

答案 0 :(得分:1)

您忘记了实际显示窗口。像

这样的函数调用
    widgetShowAll window

似乎不见了。

修改

这个主要功能应该有效:

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