views:

32

answers:

1

I have created a simple gui with curses. However, when the curses menu is finished the print function does not print anything to screen until the main program exits.

In the example below, when calc.py is run, the text "Directory list ok" is printed to the screen after the foo(calcDirs) is run. If I comment out the line folderSelection.menu(dirs) the text is printed to the screen as it normally would. Any ideas? I use python 2.5

calc.py:

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

folderSelection.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()

Edit: It seems that the text is necessarily delayed until the program terminates. It may just be delayed some 30-40 seconds.

+1  A: 

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.

Neil Baylis
Thank you very much! This did indeed solve the problem.
Pe2