Thenraja Vettivelraj
Thenraja Vettivelraj

Reputation: 256

GUI in haskell using gtk2hs and glade

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

Upvotes: 0

Views: 554

Answers (1)

Dmitry Vyal
Dmitry Vyal

Reputation: 2427

Well, seems you're mixing IO and computational parts together.

You have a pure function to do the computation you need, like so:

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

And you need to react on an event by issuing an IO action, namely, updating the state of the gui element. I assume you're trying to output the value into the same entry.

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

Please, take an attention, entryGetText/SetText work with strings, so you need to convert to and from Ints.

Upvotes: 1

Related Questions