2012-03-11 79 views
7
while 1: 
    ... 
    window.addstr(0, 0, 'abcd') 
    window.refresh() 
    ... 

window大小是全部终端大小,足够大以容纳abcd。 如果'abcd'被修改为较短的字符串,如'xyz',则在终端I上将看到'xyzd'。我究竟做错了什么?如何正确刷新curses窗口?

回答

5

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

  • 要清除字符到行的末尾,使用clrtoeol()

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

+0

它之前完成刷新或之后? – Pablo 2012-03-11 09:31:25

+0

在'refresh()'之前和'addstr()'之后(所有这些操作只会更新“虚拟”curses屏幕,直到调用refresh())。 – 2012-03-11 09:35:23

2

我使用oScreen.erase()。它清除窗口,并在0,0

2

把光标回让我们假设你有这样的代码,你只是想知道如何实现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()函数可以正常工作而不闪烁。

弗雷德里克·哈米迪擦除只是当前窗口的一部分(S)提供了以下解决方案:

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()