[Tutor] Is this the preferred way to change terminal screen color using curses?

Cameron Simpson cs at cskk.id.au
Sat Mar 2 17:27:29 EST 2019


On 02Mar2019 15:16, boB Stepp <robertvstepp at gmail.com> wrote:
>I wanted to be able to change the background screen color of a
>terminal window using curses.  My weak Google-Foo did not turn up a
>clear example of how to do this despite much searching.  The two
>_obvious_curses methods to attempt this seemed to be
>window.bkgdset(ch, attr) to initially set a window's background
>attributes and window.bkgd(ch, attr) to change them to new values.
>The thing that has puzzled me is that "ch" is a mandatory parameter to
>supply.

Remember that the Python curses module is a shim for the curses (or 
ncurses) C library. So "man 3 curses" gets you the main library page, 
and there are distinct manual entries for the various functions.

On my machine (a Mac) the curses(3) manual entry has a section named 
"Routine Name Index" thus:

   Routine Name Index
       The following table lists each curses routine and the name of the man‐
       ual page on which it is  described.   Routines  flagged  with  “*” 
       are ncurses-specific, not described by XPG4 or present in SVr4.

and then there's a long table of functions and the match manual entry 
name. bkgdset is listed as in the curs_bkgd(3) entry, so run "man 
curs_bkgd" to view that manual entry. It describes the bkgdset,  
wbkgdset,  bkgd,  wbkgd,  getbkgd functions in some detail.

Also, the curs_color manual entry talks about terminal colours in 
detail. So "man curs_color".

>So after a variety of experimental efforts I came up with the
>following approach which seems to do what I want to do -- just change
>the terminal's background color at will.  But since I did *not* locate
>any clear examples online on how to do this, I cannot help but wonder
>if there is an easier approach to do what I want to do?

I would be inclined to call getbkgd() and then modify what it gives you 
as desired, then call bkgdset(). Untested.

>My code follows.  As always I am open to any constructive criticism
>even if it is off this email's topic, though this code is not meant to
>be a polished product.
[...]
>    # Fill screen with spaces to color screen background:
>    for y in range(max_height):
>        try:
>            stdscr.addstr(y, 0, ' ' * max_width)
>        except curses.error as error:
>            if y == max_height - 1:
>                # Ignore error from writing to lower right-hand screen corner:
>                pass
>            else:
>                raise error

Is the painting of the screen with spaces actually required? I would 
have thought not (again, untested). The main window (stdscr) should 
start filled with spaces.

[Reads more closely...] probably you want to call bkgd() or wbkgd() 
instead.  "man curs_bkgd" says:

   bkgd
       The bkgd and wbkgd functions set the background property of the
       current or  specified  window  and  then  apply this setting to every
       character position in that window:
       ·   The rendition of every character on the screen is  changed to
           the new background rendition.
       ·   Wherever  the former background character appears, it is changed
           to the new background character.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list