2

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?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105

2 Answers2

2

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

  • ncurses provides resizeterm and resize_term entrypoints, but those respond to SIGWINCH (a longstanding *nix feature but only recently quasi-standardized):

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.

  • PDCurses has a resize_term function which can resize the window:

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.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Thanks, just to check if I understood right, does it mean that on Windows OS, the resizeterm does not exist in curses library and hence the AttributeError message in the question, or it exists but only works if there is a resizing signal from the user? – AmirHosein Sadeghimanesh Jun 11 '23 at 14:06
  • 1
    yes - it "could" exist if the python developers chose to provide that in their binding, but a Windows console won't provide something like `SIGWINCH`, so that would be pointless. – Thomas Dickey Jun 11 '23 at 14:10
  • The older versions of PDCurses are available on SourceForge: https://sourceforge.net/projects/pdcurses/files/pdcurses/ – William McBrine Jun 12 '23 at 02:33
  • The Windows version actually supports both kinds of resizing, depending on the version of Windows (and whether you're using the old Console or the new Terminal, which I see is not handling either kind of resizing well yet). – William McBrine Jun 12 '23 at 02:45
  • sure, but the new Terminal isn't a Console (it's emulation of Console API is incomplete), and wasn't available when PDCurses's `resize_term` was introduced. Most of the Python curses stuff far predates Windows Terminal, so while it may happen to work, that's only accidentally so. – Thomas Dickey Jun 12 '23 at 07:35
1

I still don't know what curses.resizeterm does, but if I replace it with curses.resize_term then it works as it supposed and it does what I want.

import curses
def main( stdscr ):
    curses.resize_term( 20, 20 )
    stdscr.addstr( 10, 10, "o" )
    curses.napms( 3000 )
curses.wrapper( main )

And it should be called as curses.resize_term not stdscr.resizeterm as it is a method for curses and not a window object. The result looks like the following.

enter image description here

I guess it has to do something with backened something, there is a sentence in the manual, https://docs.python.org/3/library/curses.html.

enter image description here