0

I have created a GUI using gtk2hs and glade and then passed it to haskell code in the main::IO(). Then I have some coding for the windows say for labels, buttons and entry text. e.g.,

entry         <- xmlGetWidget xml castToEntry "entry1"
applyButton   <- xmlGetWidget xml castToButton "button1"

Then after clicking on the applybutton

onClicked applyButton $ do
number <- get entry entryText

Passed the value to a variable number

Then I wrote a function for squaring the number like this

sqr :: Int -> Int -> IO ()
sqr number = number * number

after the mainGUI.

Which doesn't work!!!!!!

It is supposed to be work as

I/p: Get a number from the user in GUI

o/p: Square of the number displayed in GUI

4

1 回答 1

1

好吧,似乎您将 IO 和计算部分混合在一起。

您有一个纯函数来执行所需的计算,如下所示:

sqr :: Int -> Int -> Int
sqr number = number * number

并且您需要通过发出 IO 操作来对事件做出反应,即更新 gui 元素的状态。我假设您正在尝试将值输出到同一个条目中。

onClicked applyButton $ do
  num_str <- entryGetText entryText
  let number = read num_str
      squared = sqr number
  entrySetText entryText (show squared)

请注意,entryGetText/SetText 使用字符串,因此您需要与 Ints 相互转换。

于 2012-08-01T19:17:22.377 回答