gpt4 book ai didi

python - 在右下角调用 addch 时 curses 失败

转载 作者:太空狗 更新时间:2023-10-29 20:23:58 26 4
gpt4 key购买 nike

我开始学习 Python 中的 curses。我在 Mac OS X 上使用 Python 3.5。当我尝试在右下角写入时,程序崩溃并出现以下错误:

$ python ex_curses.py
[...]
File "ex_curses.py", line 19, in do_curses
screen.addch(mlines, mcols, 'c')
_curses.error: add_wch() returned ERR

示例程序是:

import curses

def do_curses(screen):
curses.noecho()
curses.curs_set(0)
screen.keypad(1)

(line, col) = 12, 0
screen.addstr(line, col, "Hello world!")
line += 1
screen.addstr(line, col, "Hello world!", curses.A_REVERSE)

screen.addch(0, 0, "c")

(mlines, mcols) = screen.getmaxyx()
mlines -= 1
mcols -= 1
screen.addch(mlines, mcols, 'c')

while True:
event = screen.getch()
if event == ord("q"):
break
curses.endwin()

if __name__ == "__main__":
curses.wrapper(do_curses)

我觉得我错过了一些明显的东西,但我不知道是什么。

最佳答案

这是预期的行为(一个怪癖),因为 addch 试图在添加一个字符后换行 到下一行。有一个 comment in lib_addch.c处理这个:

/*
* The _WRAPPED flag is useful only for telling an application that we've just
* wrapped the cursor. We don't do anything with this flag except set it when
* wrapping, and clear it whenever we move the cursor. If we try to wrap at
* the lower-right corner of a window, we cannot move the cursor (since that
* wouldn't be legal). So we return an error (which is what SVr4 does).
* Unlike SVr4, we can successfully add a character to the lower-right corner
* (Solaris 2.6 does this also, however).
*/

关于python - 在右下角调用 addch 时 curses 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36387625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com