简体   繁体   中英

Python Curses addwstr() returned ERR when adding lines to screen

Suppose I am adding a large number of lines to a curses screen.

Minimal non-working example:

import curses


class MyApp(object):

    def __init__(self, stdscreen):
        self.screen = stdscreen

        for i in range(0,100):

            self.screen.addstr(str(i) + '\n')
            self.screen.refresh()

        self.screen.getch()

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

The above code returns:

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    curses.wrapper(MyApp)
  File "/usr/lib/python3.7/curses/__init__.py", line 94, in wrapper
    return func(stdscr, *args, **kwds)
  File "test.py", line 11, in __init__
    self.screen.addstr(str(i) + '\n')
_curses.error: addwstr() returned ERR
Press ENTER to continue

1) What is this error? 2) If the error is because I am adding too many lines to the screen, how could I list those entries with curses? Perhaps with a scroll view of some sort?

It occurred to me that I could use try / except to determine the maximum number of lines that can be printed on the screen to avoid this error:

import curses


class MyApp(object):

    def __init__(self, stdscreen):
        self.screen = stdscreen

        maximum = self.maxlines()

        for i in range(maximum):

            self.screen.addstr(str(i) + '\n')
            self.screen.refresh()

        self.screen.getch()

    def maxlines(self):

        n = 0

        try:
            for i in range(100):
                self.screen.addstr(str(i) + '\n')
                n += 1

        except:
            pass

        self.screen.erase()

        return n


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM