Domanda

I'm writing a haskell program with GUI. When I write the following piece of code

    onClicked btn $ do
           print 1

    onClicked btn $ do
           print 2

Pressing btn resulted in printing 1 and 2 simultaneously How can I override the function definition such that the last definition replaces the first one and prints 2 only

Background: I'm writing a minesweeper game. When a button has a mine it explode, then I try to play again and define the same button to be cleared of mines, but pressing it explode a mine again because of the first definition.

Thanks

È stato utile?

Soluzione

Use the connection returned by onClicked to unregister event handlers:

print1Handler <- onClicked btn (print 1)
signalDisconnect print1Handler
print2Handler <- onClicked btn (print 2)

Also, onClicked is deprecated; when possible, you should use the new on mechanism instead. Its use is very similar:

print1Handler <- on buttonActivated btn (print 1)
signalDisconnect print1Handler
print2Handler <- on buttonActivated btn (print 2)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top