python - 如何正确刷新 curses 窗口?

标签 python refresh ncurses curses

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/

相关文章:

linux - 在 Linux 终端应用程序中接收按键和按键释放事件?

python - 如何避免使用 Python 多处理在 fork 进程中加载​​父模块

python - 在 django 中创建数组后如何删除 GET url 参数?

python - 使用 Python 在 OpenCV 中存储

ios - swift 4 刷新控件在上面

intellij-idea - 在intellij中,更改build.sbt后如何刷新项目

python - 可以从OpenCV中的视频文件中删除帧

java - 有没有办法从 applet 本身重新加载/刷新 java applet?

c - 使用退格键的 NCurses

python - curses.wrapper() 在背景/前景序列之后弄乱终端