【问题标题】:How to refresh curses window correctly?如何正确刷新诅咒窗口?
【发布时间】:2012-03-28 01:30:17
【问题描述】:
while 1:
    ...
    window.addstr(0, 0, 'abcd')
    window.refresh()
    ...

window 大小是完整的终端大小,足以容纳abcd。 如果将'abcd' 修改为更短的字符串,如'xyz',那么在终端上我将看到'xyzd'。我到底做错了什么?

【问题讨论】:

    标签: python refresh ncurses curses


    【解决方案1】:

    假设你有这段代码,你只想知道如何实现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()
    

    【讨论】:

      【解决方案2】:

      我使用oScreen.erase()。它清除窗口并将光标放回 0,0

      【讨论】:

        【解决方案3】:

        addstr() 只打印你指定的字符串,它不会清除后面的字符。你必须自己做:

        • 要清除字符直到行尾,请使用clrtoeol()

        • 要清除字符直到窗口结束,请使用clrtobot()

        【讨论】:

        • refresh() 之前和addstr() 之后(所有这些操作只会更新“虚拟”诅咒屏幕,直到调用refresh())。
        猜你喜欢
        • 2022-08-14
        • 2013-06-10
        • 2012-01-24
        • 1970-01-01
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 2019-05-18
        • 2014-10-08
        相关资源
        最近更新 更多