0

我有以下简单的脚本来演示我的问题:

#! /usr/bin/python

import curses
import time
import os

screen = curses.initscr()                                       
screen.clear()
screen.addstr(0, 0, "Hello World !!!\n", curses.A_REVERSE)      
screen.refresh()                                                
time.sleep(5)
curses.def_prog_mode()                                          #/* Save the tty modes            */
curses.endwin()                                                 #/* End curses mode temporarily   */

os.system("clear")
ans = raw_input("is the sky blue? (y/n):  ")

if ans == 'y':
        print "yes"

time.sleep(5)

curses.reset_prog_mode()                                #/* Return to the previous tty mode stored by def_prog_mode()     */
screen.refresh()                                        #/* Do refresh() to restore the Screen contents           */
screen.addstr(1, 0, "back in ncurses\n", curses.A_REVERSE)              #/* Back to curses use the full    */
screen.refresh()                                                        #/* capabilities of curses        */
time.sleep(5)
curses.endwin()                                                         #/* End curses mode               */

脚本离开 curses 模式 ok,执行 clear 命令 ok,等待用户 ok 的回答,但如果答案是 y,则输出 yes 不会打印到 stdout,直到再次启动 ncurses 模式。脚本完成后,您可以在 stdout 中看到 yes 输出,但在那之前看不到,即使使用 time.sleep(5) 命令也是如此。有没有办法在 ncurses 模式再次启动之前强制打印输出在 stdout 中显示?

4

1 回答 1

1

您需要刷新标准输出。

尝试:

import sys
sys.stdout.flush()

这应该将输出缓冲区写入终端,您应该会看到答案。

于 2013-05-22T08:32:54.150 回答