putStrLn doesn't print to console

2019-05-01 01:09发布

I am experimenting with wxHaskell. I wasn't able to run the app under ghci, so I have to use application to test it. I wanted to test the program with println debugging. However, it seems that putStrLn doesn't work in GUI:

{-# LANGUAGE Haskell2010 #-}

module Main where

import Graphics.UI.WX

drawUI dc view = do 
  circle dc (point 10 10) 5 [penKind := PenSolid, color := red]
  putStrLn "painted"  

helloGui :: IO ()
helloGui = do
  f <- frame [
    text := "Example", 
    resizeable := False, 
    bgcolor := white,
    layout := space 400 300,
    on paint := drawUI]
    return ()

main :: IO ()
main = do
  putStrLn "Started"
  start helloGui

If I comment out start helloGui, everything is printed well. However, if I return it, nothing is printed but the window is displayed. What's wrong here?

1条回答
forever°为你锁心
2楼-- · 2019-05-01 01:46

This is probably output buffering; the output is not written until the program exits.

Either flush explicitly:

  putStrLn "Started"
  hFlush stdout

Or turn on line buffering:

  hSetBuffering stdout LineBuffering -- or even NoBuffering
  putStrLn "Started"
查看更多
登录 后发表回答