-1

In the python curses documentation, it states the following:

When you call a method to display or erase text, the effect doesn’t immediately show up on the display. Instead you must call the refresh() method of window objects to update the screen.

As a result, I would expect the follow code NOT to display "hello world":

import curses
from curses import wrapper

def main(scr):
  scr.clear()
  scr.addstr("hello world!")
  scr.getkey()

wrapper(main)

And yet, running this in my terminal produces "hello world!". Why is this?

borrimorri
  • 119
  • 9
  • 1
    I think that all of the `curses` input functions automatically refresh the screen - you wouldn't be able to see the character echo otherwise. – jasonharper Feb 26 '23 at 03:10
  • @jasonharper it looks like you are correct! Docs say: "getch() refreshes the screen and then waits for the user to hit a key". Pretty misleading because the same page says "... All you have to do is to be sure that the screen has been redrawn before pausing to wait for user input, by first calling stdscr.refresh() or the refresh() method of some other relevant window." – borrimorri Feb 26 '23 at 03:35

2 Answers2

0

Even though the refresh() method is not called explicitly, the text is still displayed on the screen. This is because the addstr() method implicitly calls the refresh() method internally. So the text is added to the screen and displayed without the need for an explicit call to refresh(). However, it's a good practice to call refresh() after making changes to the screen to ensure that they are immediately updated.

Vikesh K
  • 1
  • 1
  • 1
    I don't think this is true. Calling `scr.addstr("hello world!")` and then `curses.napms(2000)` results in the text not appearing. Calling `scr.refresh()` in between the calls makes the text show. It appears that `scr.getkey()` is actually the method that calls `refresh()` internally. – borrimorri Feb 26 '23 at 03:52
0

As per the docs, getch() (and other input functions) internally refreshes the screen.

getch() refreshes the screen and then waits for the user to hit a key

borrimorri
  • 119
  • 9