Search code examples
pythonpython-3.xcursespython-curses

Text added to New window is not visible in Curses


I am trying to add a window and a text in this window with curses using this and this:

window.addstr("This is a text in a window")

Code:

class View:
    def __init__(self, ):
        self.stdscr = curses.initscr()
        curses.noecho()
        curses.cbreak()
        self.stdscr.keypad(True)
        # -----------------
        self.add_window_and_str()
        self.add_str()
        # -----------------
        self.stdscr.getkey()
        curses.endwin()

    def add_str(self): #just text in standart screen
        self.stdscr.addstr("test")
        self.stdscr.refresh()

    def add_window_and_str(self):
        scr_limits = self.stdscr.getmaxyx()
        win = curses.newwin(scr_limits[0] - 10, scr_limits[1] - 10, 5, 5)
        win.addstr("Example String")
        win.refresh()
        self.stdscr.refresh()

The text added with self.add_str is visible but the "Example String" is not. How can i manipulate windows to make that text visible?


Solution

  • On initialization, the standard screen has a pending update (to clear the screen). The refresh call at the end of add_window_and_str does that, overwriting the win.addstr output. You could move that call before the first call to add_window_and_str. After that, the changes to stdscr would be in parts of the screen outside your window.

    There's another problem: calling getch refreshes the associated window. Usually programs are organized so that getch is associated with whatever window you would like to keep "on top", so that their updates will not be obscured by other windows. If you return the win variable from add_window_and_str, you can use that window with getch.