当addtr为9,9时,ncurses 10,10 pad会导致错误

时间:2013-05-26 21:01:42

标签: python ncurses python-curses

在宣布10×10的垫后,我显然无法写入右下角,9,9没有错误。这是怎么回事?

import curses

def start(stdscr):
    curses.curs_set(0)
    movement = curses.newpad(10, 10)

    movement.addstr(8, 9, '+')
    movement.addstr(9, 8, '+') 
    movement.addstr(9, 9, '+') #This line causes an error

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

导致:

  File "curses_movement_testing.py", line 35, in <module>
    curses.wrapper(start)
  File "/usr/lib64/python2.6/curses/wrapper.py", line 43, in wrapper
    return func(stdscr, *args, **kwds)
  File "curses_movement_testing.py", line 10, in start
    movement.addstr(9, 9, '+')
_curses.error: addstr() returned ERR

为什么我不能写到右下角的单元格?

1 个答案:

答案 0 :(得分:6)

问题是添加的角色导致尝试滚动打击垫,并且滚动被禁用。该角色已成功添加(至少在我的系统的ncurses实现中):如果您发现错误并更新屏幕,您应该看到它:

import curses, time

def start(stdscr):
    curses.start_color()
    try:
        curses.curs_set(0)
    except curses.error:
        pass
    movement = curses.newpad(10, 10)

    movement.addstr(8, 9, '+')
    movement.addstr(9, 8, '+')
    try:
        movement.addstr(9, 9, '+')
    except curses.error:
        movement.addstr(0, 0, 'CAUGHT')
    movement.refresh(0, 0, 0, 0, 9, 9)
    curses.doupdate()
    time.sleep(1)

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

如果您在写入右下角之前致电movement.scrollok(True),则不会出现错误,但窗口会滚动:可能不是您想要的。

由于垫可能比实际屏幕大,因此一个简单的解决方案是在refresh部分下方添加额外的线。您可以检查是否已写入不会显示的部分。这是最后一个例子:

import curses, sys, time

def start(stdscr):
    curses.start_color()
    try:
        curses.curs_set(0)
    except curses.error:
        pass
    movement = curses.newpad(11, 10)

    movement.addstr(8, 9, '+')
    movement.addstr(9, 8, '+')
    text = sys.argv[1] if len(sys.argv) > 1 else '+'
    try:
        movement.addstr(9, 9, text)
        y, x = movement.getyx()
    except curses.error:
        y, x = movement.getyx()
        movement.addstr(0, 0, 'CAUGHT')
    if y >= 10 and x > 0:
        movement.addstr(1, 0, 'toolong')
    movement.refresh(0, 0, 0, 0, 9, 9)
    curses.doupdate()
    time.sleep(1)

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

python cursesx.py运行并显示优势;以python cursesx.py foo运行,并显示ftoolong条消息;以python cursesx.py waytoolongtofit运行,wCAUGHTtoolong消息全部显示。