7

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.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260

1 Answers1

3

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.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
  • Just what I needed. I didn't think it was that easy. Do not forget, to set your y cursor variable (if you are increasing one with every line) by one back. – Erich Kuester Jul 25 '23 at 12:58