How to update curses window correctly?

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

window size - the full size of the terminal, large enough to hold abcd . If 'abcd' changed to a shorter string, for example 'xyz' , then on the terminal I will see 'xyzd' . What exactly am I doing wrong?

+7
source share
3 answers

addstr () prints only the specified string; it does not clear the following characters. You will have to do it yourself:

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

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

+5
source

I am using oScreen.erase() . It clears the window and returns the cursor to 0.0

+2
source

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 simplest and “curse-ish” solution is, of course,

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

You may be tempted to write this instead:

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

But do not! Despite the friendly name, clear() is only valid for when you want the entire screen to be redrawn unconditionally , i.e. Flicker, The erase() function does the right thing without flickering.

Frédéric Hamidi offers the following solutions to erase only 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 cleaner Python alternative would be

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

All Articles