5

I'm working on python curses and I have an initial window with initscr(). Then I create several new windows to overlap it, I want to know if I can delete these windows and restore the standard screen without having to refill it. Is there a way? Could someone tell me the difference between a window, subwindow, pad and sub pad.

I have this code:

stdscr = curses.initscr()
####Then I fill it with random letters
stdscr.refresh()
newwin=curses.newwin(10,20,5,5)
newwin.touchwin()
newwin.refresh()

####I want to delete newwin here so that if I write stdscr.refresh() newwin won't appear

stdscr.touchwin()
stdscr.refresh()

####And here it should appear as if no window was created.
lesolorzanov
  • 3,536
  • 8
  • 35
  • 53

1 Answers1

11

This, e.g, should work:

import curses

def fillwin(w, c):
    y, x = w.getmaxyx()
    s = c * (x - 1)
    for l in range(y):
        w.addstr(l, 0, s)

def main(stdscr):
    fillwin(stdscr, 'S')
    stdscr.refresh()
    stdscr.getch()

    newwin=curses.newwin(10,20,5,5)
    fillwin(newwin, 'w')
    newwin.touchwin()
    newwin.refresh()
    newwin.getch()
    del newwin

    stdscr.touchwin()
    stdscr.refresh()
    stdscr.getch()

curses.wrapper(main)

This fills the terminal with 'S'; at any keystoke, it fills the window with 'w'; at the next keystroke, it removes the window and show the stdscr again, so it's again all-'S'; at the next keystroke, the script ends and the terminal goes back to normal. Isn't this working for you? Or do you actually want something different...?

sdaau
  • 36,975
  • 46
  • 198
  • 278
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Hey! thanks :D, well the only thing I really needed was de "del" I was not sure how to delete the windows, and it worked inside the code I had, thanks really. – lesolorzanov Apr 06 '10 at 03:30
  • I was wandering - could one use `newwin = None`,instead of `del newwin` (so one could test for `newwin`, without raising `UnboundLocalError` or similar)? That is, would the Python garbage collector properly dispose of the `newwin` resources, if just `newwin = None` is specified? – sdaau May 13 '13 at 07:15
  • 1
    For all intents and purposes, "del newwin" is functionally equivalent to "newwin = None". That is, "del" does not actually *delete* newwin; it merely cleans up that particular reference, reducing its reference count by one. – rdb Jan 13 '14 at 15:59
  • Deleting newwin has no effect (see manpage for [delwin](https://invisible-island.net/ncurses/man/curs_window.3x.html#h3-delwin)). The following touchwin/refresh of stdscr is all that's actually effective. – Thomas Dickey Jun 01 '22 at 22:45