-1

Considering the following curses console program in python. It should print the key variable on the terminal. However, there is also a window active so when print() invokes it really messes up the entire screen.

I would like the print to occur in a predictable place so at the end of the game I can output some results to the non-curses terminal.

What is the right way to use python print() and curses in single program?

import curses

screen = curses.initscr()
h, w = screen.getmaxyx()
window = curses.newwin(10, 20, 0, 0)

window.keypad(1)
curses.curs_set(0)
window.border(0)
window.timeout(100)

inputs = [];
while True:
    key = window.getch()
    inputs.append(key)
    print(key) # <---- this messes things up

print(f"You typed: ${inputs}") # <---- this messes things up

In practice, the print() is for debug logging and other variable outputs that I need for the game.

Thank you

I tried using window.addstr() function to output the results. But I'm after how to utilise print for system output at the end of the game rather than modifying the console window.

1 Answers1

0

curses' output stream and print stream write by default to the same device (e.g., your terminal), but are buffered independently (see release notes for ncurses 6.0 in 2015).

You can mix the two by temporarily suspending curses mode. For instance, you may find these helpful:

(getch does a refresh, so you should be able to use that instead of an explicit refresh).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105