سؤال

I need to represent words in label (or other display type widget) sequentially

, using gtk2hs.

I tried this:

import Graphics.UI.Gtk
import Control.Concurrent

main = do 
    initGUI
    window <- windowNew
    window `onDestroy` mainQuit

    lay <- vBoxNew False 0

    but <-buttonNewWithLabel "Sequently show one, two and three"

    lab <- labelNew Nothing

    onPressed but $ do
        labelSetText lab "one"
        threadDelay 1000000
        labelSetText lab "two"
        threadDelay 1000000
        labelSetText lab "three"

    containerAdd window lay
    containerAdd lay but
    containerAdd lay lab

    widgetShowAll window
    onDestroy window mainQuit
    mainGUI

I think, that it should show "one" in label, wait 1 second, show "two", wait one second, and then show "three", when I press the button. But this only shows "three" in label after 2 seconds after I pressed the button.

I also tried Statusbar, which gives same result.

P. S.: Maybe it's windows-specific?

P. P. S.: gtk2hs is 0.12.4

هل كانت مفيدة؟

المحلول

The solution is to run new thread!

We need to put forkIO in the body of onPressed:

onPressed but $ do
    forkIO $ do
        set lab [labelText:="one"]
        threadDelay 1000000
        set lab [labelText:="two"]
        threadDelay 1000000
        set lab [labelText:="three"]
    return()

It works fine now!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top