Question

I'm using curses in Python3.3 and need to fill the entire usable space with characters. The error I'm getting occurs when my double for loop reaches the last corner.

Traceback (most recent call last):
  File "main.py", line 14, in <module>
    curses.wrapper(main)
  File "/usr/local/lib/python3.3/curses/__init__.py", line 94, in wrapper
    return func(stdscr, *args, **kwds)
  File "main.py", line 9, in main
    stdscr.addch(y, x, ord('.'))
_curses.error: addch() returned ERR

I've noticed that each time a character is added, the cursor moves to the right. I haven't tested this as thoroughly as I might have, but I suspect that the cursor leaves the end of the window on that last call to stdscr.addch, which likely causes the error.

An additional note is that by using a maximum width or height one unit under what the window returns, I'm able to loop without an error.

Source that fails:

import curses

def main(stdscr):
    height, width = stdscr.getmaxyx()
    for y in range(height):
        for x in range(width):
            stdscr.addch(y, x, ord('.'))
    stdscr.refresh()
    stdscr.getch()

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

For my project, it's important that I use a complete rectangle. Up to this point I've shortened my max width from 80 to 79, but if anyone knows a way to write to that last corner, I'd like to hear it.

Was it helpful?

Solution

Calling insch in corner instead of addch worked for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top