terminalncursespython-cursespdcurses

How to fix the width and height of the window using curses in Python?


According to this link, https://docs.python.org/3/library/curses.html, the description for curses.resizeterm(), it should be possible to fix the width and height of my terminal window to a fixed values of my interest, no? I wrote a simple code below

import curses

def main( stdscr ):
    stdscr.resizeterm( 20, 20 )
    stdscr.clear()
    stdscr.addstr( 10, 10, "o" )
    stdscr.refresh()
    curses.napms( 3000 )
    
curses.wrapper( main )

But it returns the error message AttributeError: '_curses.window' object has no attribute 'resizeterm'. If I remove the resizeterm line, it works fine, but of course the size of the screen is not necessarily 20 by 20! If I put it in front of curses instead of stdscr, then it gives a similar error message again AttributeError: module 'curses' has no attribute 'resizeterm'. How should one use this resizeterm?


Solution

  • Python's documentation needs some work, but basically the problem is that it's used for two different underlying implementations:

    The function resizeterm resizes the standard and current windows (i.e., stdscr and curscr) to the specified dimensions, and adjusts other bookkeeping data used by the ncurses library that record the window dimensions such as the LINES and COLS variables.

    resize_term() is effectively two functions: When called with nonzero values for nlines and ncols, it attempts to resize the screen to the given size. When called with (0, 0), it merely adjusts the internal structures to match the current size after the screen is resized by the user. On the currently supported platforms, SDL, Windows console, and X11 allow user resizing, while DOS, OS/2, SDL and Windows console allow programmatic resizing. If you want to support user resizing, you should check for getch() returning KEY_RESIZE, and/or call is_termresized() at appropriate times; if either condition occurs, call resize_term(0, 0). Then, with either user or programmatic resizing, you’ll have to resize any windows you’ve created, as appropriate; resize_term() only handles stdscr and curscr.

    PDCurses's resize_term (1998, version 2.3) came after ncurses's resizeterm (1995), and rather than implementing the same feature, modified that (to resize the window rather than responding to the user's resizing it). ncurses added a resize_term in 2002 after that to solve a different problem. The Git repo for PDCurses, by the way, only goes back to version 2.4 (I have tarballs).

    PDCurses's documentation also needs some work, but the X11 port does respond to SIGWINCH (hence the KEY_RESIZE which it adapted from ncurses), but resizing the window rather than responding to SIGWINCH was what it did for the Windows version.