I have a simple python curses code that creates a subwindow. However, in the process of running the function window.subwin() fails with the message:
Here a test case:
import curses
if __name__ == '__main__':
curses.initscr()
window = curses.newwin(15, 40, 7, 20)
window.box()
window.refresh()
subwindow = window.subwin(5, 10, 2, 2)
subwindow.box()
subwindow.refresh()
subwindow.getkey()
curses.endwin()
produces the following output:
Traceback (most recent call last):
File "c.py", line 12, in <module>
subwindow = window.subwin(5, 10, 2, 2)
_curses.error: curses function returned NULL
Is there any way to get a more descriptive message?
The error likely happens when it is not possible to create a sub-window (illegal operation). This can happen because you are asking to paint the sub-window outside of the window.
The method subwin receives absolute coordinates (with respect to the screen, not the parent window). It will fail if the subwin coordinates are outside of window. Another reason to fail: the width or height overflows the window.
Instead of subwin, you can use derwin (derivated window), which receives relatives coordinates (less prone to error).
import curses
if __name__ == '__main__':
curses.initscr()
window = curses.newwin(15, 40, 7, 20)
window.box()
window.refresh()
subwindow = window.derwin(5, 10, 2, 2) # <- here is the change
subwindow.box()
subwindow.refresh()
subwindow.getkey()
curses.endwin()