Is there a good reason why you use the subtitle? If you create a new top-level window, the code works correctly - just change stdscr.subwin to curses.newwin and it works as you expected.
I am not a curse expert, but I believe that subwindow shares its character buffer with its parent in such a way that changes to one will also affect the other. So, if you want to divide the window into logical areas (perhaps a menu bar, main area and status bar), then auxiliary windows are useful. If, however, you are looking for something more, like a dialog box or a pop-up menu, then an all-new window (with its own separate buffer) is what you need.
I cannot find the final link to ncurses that agrees or disagrees with me, but the man page for AIX seems to confirm this:
Recall that the subtitle has its parent window buffer. Changes made to the general window buffer in the area covered by the subtitle through the parent window or any of its subwindows affect all windows sharing the window buffer.
Of course, this is not final for ncurses, but I cannot find anything contrary, and it seems to explain the observed behavior. I also did a rough experiment, where right after the subwin.getch() in your example, I added this line:
raise Exception(stdscr.instr(20, 15, 3))
In your example, I get JJJ as the contents of the actual main window. If I use curses.newwin() to create a window instead of stdscr.subwin() , I get the expected MMM .
I donβt know how many specific Python curse resources exist, but most of the standard ncurses tutorials and documents are very useful at this level. Back when I needed to do some work, this document was quite useful. If you go to the βExampleβ section, you will see that the menu popups are not windows - he refers to this with the following slightly vague explanation:
We do not want this new window to overwrite previously written characters in the background. They should stay there after closing the menu. This is why a menu window cannot be created as a substring of stdscr.
In addition, I remember that using both stdscr and your own windows can cause problems - the official introduction of ncurses has some warnings about this. He also suggests avoiding overlapping windows completely, as they seem to be error prone, but I donβt remember having any problems with them for short-term transient modal dialogs (this is the only use I made to them). Of course, just because my simple use case did not reveal any problems, this does not mean that they are not. However, in something as complex as ncurses, I see the wisdom in keeping things as simple as you can.
Hope some help. As I said, I am by no means an expert on curses, but I hope this is a few more steps forward.