Search code examples
pythonterminalcurses

curses.wrapper() isn't working [python]


I am trying to sort out things quickly with curses.wrapper() function. As the docs say, it accepts a function or object whose first argument will be the screen which then used to further work on the screen. This is the code:

def function(screen):
    screen.addstr(0, 0, "print something here")
    screen.refresh()

curses.wrapper(function)

But, nothing prints back on the screen. Control just goes in and out.


Solution

  • Your code is (or at least could be…) working just fine; it just finishes so fast you never get a chance to see it.

    You put a string on the screen with addstr and refresh. Then you immediately return from function, which returns from wrapper, which restores the terminal to its pre-curses state, so the results vanish.

    If you want to see something happen, you'll need to put in some kind of delay. For example:

    def function(screen):
        screen.addstr(0, 0, "print something here")
        screen.refresh()
        screen.getkey()
    

    Now it'll display "print something here" in the top left, then wait for you to press a key, during which time you can see that string.