How to refresh curses window correctly?

≡放荡痞女 提交于 2019-12-14 00:18:47

问题


while 1:
    ...
    window.addstr(0, 0, 'abcd')
    window.refresh()
    ...

window size is full terminal size, big enough to hold abcd. If 'abcd' is modified to shorter string like 'xyz', then on terminal I will see 'xyzd'. What exactly I am doing wrong?


回答1:


addstr() only prints the string you specify, it does not clear the following characters. You will have to do that yourself:

  • To clear characters until the end of the line, use clrtoeol(),

  • To clear characters until the end of the window, use clrtobot().




回答2:


Let's suppose you have this code, and you just want to know how to implement draw():

def draw(window, string):
    window.addstr(0, 0, string)
    window.refresh()

draw(window, 'abcd')
draw(window, 'xyz')  # oops! prints "xyzd"!

The most straightforward and "curses-ish" solution is definitely

def draw(window, string):
    window.erase()  # erase the old contents of the window
    window.addstr(0, 0, string)
    window.refresh()

You might be tempted to write this instead:

def draw(window, string):
    window.clear()  # zap the whole screen
    window.addstr(0, 0, string)
    window.refresh()

But don't! Despite the friendly-looking name, clear() is really only for when you want the entire screen to get redrawn unconditionally, i.e., "flicker". The erase() function does the right thing without flicker.

Frédéric Hamidi offers the following solutions for erasing just part(s) of the current window:

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

A shorter and pure-Python alternative would be

def draw(window, string):
    window.addstr(0, 0, '%-10s' % string)  # overwrite the old stuff with spaces
    window.refresh()



回答3:


I use oScreen.erase(). It clears the window and puts the cursor back at 0,0



来源:https://stackoverflow.com/questions/9653688/how-to-refresh-curses-window-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!