while 1:
...
window.addstr(0, 0, 'abcd')
window.refresh()
...
window
大小是完整的终端大小,大到足以容纳 abcd
。
如果 'abcd'
被修改为较短的字符串,如 'xyz'
,那么在终端上我将看到 'xyzd'
。我到底做错了什么?
最佳答案
假设您有这段代码,您只想知道如何实现draw()
:
def draw(window, string):
window.addstr(0, 0, string)
window.refresh()
draw(window, 'abcd')
draw(window, 'xyz') # oops! prints "xyzd"!
最直接和“诅咒式”的解决方案绝对是
def draw(window, string):
window.erase() # erase the old contents of the window
window.addstr(0, 0, string)
window.refresh()
您可能想改为这样写:
def draw(window, string):
window.clear() # zap the whole screen
window.addstr(0, 0, string)
window.refresh()
但是不要!尽管名称看起来很友好,但 clear()
实际上只适用于 when you want the entire screen to get redrawn unconditionally,即,“闪烁”。 erase()
函数在没有闪烁的情况下做了正确的事情。
Frédéric Hamidi 提供了以下用于删除当前窗口的一部分的解决方案:
def draw(window, string):
window.addstr(0, 0, string)
window.clrtoeol() # clear the rest of the line
window.refresh()
def draw(window, string):
window.addstr(0, 0, string)
window.clrtobot() # clear the rest of the line AND the lines below this line
window.refresh()
一个更短的纯 Python 替代方案是
def draw(window, string):
window.addstr(0, 0, '%-10s' % string) # overwrite the old stuff with spaces
window.refresh()
关于python - 如何正确刷新 curses 窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9653688/