1

我用诅咒创建了一个简单的 gui。但是,当curses 菜单完成时,打印功能不会在主程序退出之前将任何内容打印到屏幕上。

在下面的示例中,当运行 calc.py 时,在运行 foo(calcDirs) 后,文本“Directory list ok”会打印到屏幕上。如果我注释掉该行folderSelection.menu(dirs),则文本会像往常一样打印到屏幕上。有任何想法吗?我使用python 2.5

计算.py:

import folderSelection
[...]
calcDirs=folderSelection.menu(dirs)
print "Directory list ok"
foo(calcDirs)

文件夹选择.py:

import curses
def menu(folders):
    global scr
    scr = curses.initscr()
    curses.noecho()      # Do not echo keypresses
    curses.cbreak()      # No enter required
    scr.keypad(1)   # Support keypad
    curses.curs_set(0)   # Do not show the cursor

    # Do some calculations
    [...]

    exitCurses()
    return calcDirs

def exitCurses():
    global scr
    curses.nocbreak()
    curses.curs_set(1)
    scr.keypad(0)
    curses.echo()
    curses.endwin()

编辑:似乎文本必须延迟到程序终止。它可能只是延迟了大约 30-40 秒。

4

1 回答 1

1

I ran into a similar problem. It seems that curses does something to the output buffering on stdout. I think it's increasing the output buffer size, or setting buffered output mode.

Reopening stdout with a buffer size of zero may fix it.

sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

Try that after curses returns but before you print anything.

于 2010-09-14T05:00:26.273 回答