在Gtk2HS中创建一个ComboBox小部件

时间:2017-08-01 19:09:35

标签: haskell combobox gtk2hs

我想用以下代码创建一个ComboBox小部件:

void initGUI
  window <- windowNew

  ...

  cb <- comboBoxNewText
  comboBoxAppendText cb "Option 1"
  comboBoxAppendText cb "Option 2"
  comboBoxSetActive cb 0
  boxPackStart hb cb PackNatural 0

  ...

但是出现了这个错误:

Couldn't match type ‘[Char]’
                   with ‘text-1.2.2.0:Data.Text.Internal.Text’
    Expected type: ComboBoxText
      Actual type: [Char]
    In the second argument of ‘comboBoxAppendText’, namely
      ‘"Secuencial"’
    In a stmt of a 'do' block: comboBoxAppendText cb "Secuencial"
    In the expression:
      do { void initGUI;
           window <- windowNew;
           set
             window
             [windowTitle := "A title",
              windowDefaultWidth := 1024, ....];
           vb <- vBoxNew False 7;
           .... }

我只是关注本教程(http://www.muitovar.com/gtk2hs/chap4-2.html)并阅读文档(http://projects.haskell.org/gtk2hs/docs/gtk2hs-docs-0.9.12/Graphics-UI-Gtk-ModelView-ComboBox.html#v%3AcomboBoxInsertText

如何才能让它发挥作用?

提前致谢。

1 个答案:

答案 0 :(得分:2)

我建议使用documentation on Hackage。您链接的文档可能已经过去十年了。

From that documentation,我们有

type ComboBoxText = Text
comboBoxAppendText :: ComboBoxClass self => self -> ComboBoxText -> IO Int

您正在将"Option 1"作为ComboBoxText参数传递。在vanilla Haskell中,这是一个String而不是Text - 正如错误所说的那样。您可以pack String,如

import qualified Data.Text as T
comboBoxAppendText cb (T.pack "Option 1")

或启用OverloadedStringsString文字自动完成此操作,如:

{-# LANGUAGE OverloadedStrings #-}
comboBoxAppendText cb "Option 1"