pythonwindowscursespython-curses

curses.addstr does not output to console


I've recently installed the windows-curses module on my machine, via py -m pip install windows-curses run in an elevated command prompt.

I wrote the following code to test if it was working correctly. If it runs correctly, it should print the string test to the console. However, nothing happens - i.e the terminal remains blank, with no characters displayed.

#initialise curses
import curses
stdscr=curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)

#write text
stdscr.addstr(0,0,'test')

#loop program so the terminal does not close
while 1:
    pass

I've tried putting the addstr call inside the while loop, as well as using the wrapper, but neither resolved the issue. What's going on, and how can I resolve this issue? Thanks in advance!


Solution

  • You have to refresh the screen. Curses does a physical screen update when you ask for input, e.g. with window.getch() or call window.refresh() directly. Try this snippet:

    #write text
    stdscr.addstr(0, 0, 'test')
    
    #loop program so the terminal does not close
    while 1:
        c = stdscr.getch()
        key = curses.keyname(c)
        if key == b'q':
            break
        else:
            stdscr.addstr(1, 0, repr(key))
    
    curses.endwin()