putStrLn不打印到控制台 [英] putStrLn doesn't print to console

查看:144
本文介绍了putStrLn不打印到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验wxHaskell。我无法在ghci下运行应用程序,所以我必须使用应用程序来测试它。我想用println调试来测试程序。但是,似乎putStrLn在GUI中不起作用:

  { - #LANGUAGE Haskell2010# - } 

模块Main其中

import Graphics.UI.WX

drawUI dc view = do
circle dc(点10 10)5 [penKind:= PenSolid ,color:= red]
putStrLn画

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

main :: IO()
main = do
putStrLnStarted
start helloGui

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

解决方案

这可能是输出缓冲;

显式清除:

  putStrLnStarted
hFlush stdout

或者打开行缓冲:

  hSetBuffering stdout LineBuffering  - 甚至NoBuffering 
putStrLnStarted


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?

解决方案

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"

这篇关于putStrLn不打印到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆