python-3.x - 如何填充到python-curses中的右下角

标签 python-3.x python-curses

我在 Python3.3 中使用 curses,需要用字符填充整个可用空间。当我的双 for 循环到达最后一个角落时,我得到的错误发生了。

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

我注意到每次添加一个字符时,光标都会向右移动。我没有尽可能彻底地测试它,但我怀疑光标在最后一次调用 stdscr.addch 时离开了窗口的末尾,这可能会导致错误。

另外要注意的是,通过使用窗口返回值下方一个单位的最大宽度或高度,我能够无错误地循环。

失败的来源:

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)

对于我的项目,使用完整的矩形很重要。到目前为止,我已将最大宽度从 80 缩短到 79,但如果有人知道写到最后一个角的方法,我想听听。

最佳答案

在角落里调用 insch 而不是 addch 对我有用。

关于python-3.x - 如何填充到python-curses中的右下角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21594778/

相关文章:

python-3.x - LU 分解速度与传统 Ax=b 的比较

file - 使用 Python 3 和 unicode 写入文件

python - Curses 菜单模块有自己的方法

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

python - 终止进程会打破Python的诅咒

python - 是否可以检查列表中的项目是否存在于变量中?

django - 嵌套序列化程序 django rest 框架中的上下文

plugins - 基于Python的浏览器插件?

python-curses - 使用 npyscreen 在表单之间传递值