pythonpython-curses

Curses does not print output


I am new to curses so am a little confused why I don't see any output from this little "hello world":

import curses
import time


def main(window: curses.window):
    window.addstr(1, 1, "Hello, world")
    time.sleep(3)


if __name__ == "__main__":
    curses.wrapper(main)

When I ran, the screen was blank for a couple of seconds, but I did not see the text at coordinate (1, 1) at all. After that, the screen was restored to what it was before running the script.

How do I fix this so the text shows?


Solution

  • Curses buffers up all the changes so the user doesn't see all the incremental differences, and shows them when you call window.refresh().

    import curses
    import time
    
    def main(window: curses.window):
        window.addstr(1, 1, "Hello, world")
        window.refresh()
        time.sleep(3)
    
    if __name__ == "__main__":
        curses.wrapper(main)