pythonscrollpython-curses

How to scroll with curses?


How to scroll with curses? I tried the following, but it fails:

import curses

def main(stdscr):

    stdscr.clear()

    # Display 10 numbered lines:
    for line in range(10):
        stdscr.addstr(line, 0, str(line))

    stdscr.getch()  # Wait for a key press

    # Scrolling:
    stdscr.setscrreg(0, 9)  # Set scrolling region
    for _ in range(5):
        stdscr.scroll()  # Fails!
        stdscr.getch()

curses.wrapper(main)

The error does not give much information:

    stdscr.scroll()
_curses.error: scroll() returned ERR

I tried both with the Terminal application in OS X and in an xterm (OS X too), but the error is the same in both cases.


Solution

  • Alright: using stdscr.scrollok(True) before scrolling works (I thought that I had tried it, but apparently that was in a different context).

    It thus seems that scroll() did something to the cursor that moved it beyond the bottom of the window.