我在 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/