【问题标题】:putStrLn doesn't print to consoleputStrLn 不打印到控制台
【发布时间】:2012-09-15 08:25:50
【问题描述】:

我正在尝试使用 wxHaskell。我无法在 ghci 下运行应用程序,所以我必须使用应用程序来测试它。我想用 println 调试来测试程序。但是,putStrLn 似乎在 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

如果我注释掉 start helloGui,一切都会打印好。但是,如果我返回它,则不会打印任何内容,但会显示窗口。这里有什么问题?

【问题讨论】:

  • 输出缓冲?关闭窗口后是否立即打印?
  • 是的,我用一个打印和刷新的函数来解决这个问题。但是有什么办法可以开启自动刷新?

标签: haskell wxhaskell


【解决方案1】:

这可能是输出缓冲;在程序退出之前不会写入输出。

显式刷新:

  putStrLn "Started"
  hFlush stdout

或者开启行缓冲:

  hSetBuffering stdout LineBuffering -- or even NoBuffering
  putStrLn "Started"

【讨论】:

    猜你喜欢
    • 2012-02-07
    • 2015-05-04
    • 2016-11-19
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多