how to set the background color of a window with curses
Jim Dennis
jimd at vega.starshine.org
Sun Mar 17 19:08:41 EST 2002
In article <mailman.1015708997.5233.python-list at python.org>, Marco Herrn wrote:
> On Sat, Mar 09, 2002 at 05:42:04PM +0100, Gerhard H?ring wrote:
>> Le 09/03/02 ? 13:53, Marco Herrn ?crivit:
>>> Hi,
>>> I am trying to set a backround color for a window and did it with
>>> stdscr.bkgd(' ', curses.COLOR_BLUE)
>>> But that doesn't work. It seems to do nothing.
>>> What am I doing wrong?
>> You're confusing colors with attributes. I recently stumbled across that
>> one, too :)
> Hmm, that may be true.... ;)
>> This will do the trick:
>> # define color 1 as black on blue
>> curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_BLUE)
>> stdscr.bkgd(' ', curses.color_pair(1))
> No, that doesn't work for me.
> The whole screen remains black. I get no error message and any text that
> is displayed with color_pair(1) is correctly displayed in black on blue.
> Am I missing something else?
>> Btw. there's a curses howto on http://py-howto.sf.net/ in case you
>> didn't know.
> Yes, I did read it. It is a good starting point, but unfortunately
> doesn't go over my problem ;)
I don't think that the Python Curses HOWTO is a good starting point
for curses, especially regarding the use of color in ncurses.
Here's a working example of color and attributes in curses:
#!/usr/bin/env python2.2
""" Curses Clock: draw a colorful clock in the center of a terminal
and update it every second. Quit on first keystroke
Bugs: handles window change (SIGWINCH) crudely
abends (exit code 1) if not curses.has_colors(), no error message
mouse events are so totally not working!
(may be cause of segfault?)
by James T. Dennis <jimd at starshine.org>
Sun Mar 17 15:06:52 PST 2002
"""
import curses, time, random, string, sys
def main(w):
w.keypad(1)
w.leaveok(1)
w.immedok(0)
w.nodelay(1)
w.scrollok(0)
w.erase()
norm = curses.A_NORMAL
bold = curses.A_BOLD
dim = curses.A_DIM
if curses.has_colors():
colorlist = (("red", curses.COLOR_RED),
("green", curses.COLOR_GREEN),
("yellow", curses.COLOR_YELLOW),
("blue", curses.COLOR_BLUE),
("cyan", curses.COLOR_CYAN),
("magenta", curses.COLOR_MAGENTA),
("black", curses.COLOR_BLACK),
("white", curses.COLOR_WHITE))
colors = {}
colorpairs = 0
for name,i in colorlist:
colorpairs += 1
curses.init_pair(colorpairs, i, curses.COLOR_BLACK)
colors[name]=curses.color_pair(i)
else:
sys.exit(1)
## (mousecap, mouseold) = curses.mousemask(0)
## (mousecap, mouseold) = curses.mousemask(mouseold | mousecap)
tpart = { "day": 0, "month": 4, "date": 8, "hour": 11,
"min": 14, "sec": 17, "year": 21 }
key = 0
while 1:
if key == 0 or key == 410: # 410 is from SIGWINCH?
w.erase()
y,x=w.getmaxyx()
mid = y / 2
ctr = x / 2
t = time.asctime()
ctr -= len(t) / 2
### k = w.getkey()
### Warning: previous line causes segfault!
key = w.getch()
## if key == 410: continue
if key == ord('q') or key == ord('Q'):
break
else:
w.addstr(0, 0, "\t%s\t" % key, colors["yellow"])
### w.addstr(0, 0, "\t%s\t" % curses.keyname(k), colors["yellow"])
### Warning: previous line causes segfault!
t = time.asctime()
w.addstr(mid,ctr+tpart["day"], t[0:3],colors["blue"] | norm)
w.addstr(mid,ctr+tpart["month"],t[4:8],colors["blue"] | dim)
w.addstr(mid,ctr+tpart["date"], t[8:11],colors["blue"] | bold)
w.addstr(mid,ctr+tpart["hour"], t[11:14],colors["red"] | dim)
w.addstr(mid,ctr+tpart["min"], t[14:17],colors["red"] | bold)
w.addstr(mid,ctr+tpart["sec"], t[17:20],colors["green"] | dim)
w.addstr(mid,ctr+tpart["year"], t[20:],colors["magenta"] | dim)
time.sleep(1)
return key
if __name__ == "__main__":
x = curses.wrapper(main)
print x
This may not be efficient, but it shows the basics of handling color and
attributes. BTW: the "dim" attribute looks just like "normal" on my
terminals (xterms and Linux console). So that may not be useful in most
cases.
As you can see it isn't at all clever about terminals that lack ncurses
color support, and it can't handle dynamic window size changes (SIGWINCH)
Separately I'm playing with the signal module to see if I can add SIGWINCH
support my own version of curses.wrapper. (Although it seems that
getch() returns 410 on window resize events). I've also been mentally
playing with possible ways to create an enhanced curses.wrapper with
support for passing arguments to main() and return values back from
it. (Of course I can simply create a global mutable "retval = []" and
do an assignment to that prior to a return. Actually it seems I can
call: r = curses.wrapper(foo) and get the return value from foo().
Also NOTE the two lines where I get python to segfault (this is
Python2.2 from Debian "Unstable") but it does it under 2.1, too.
(It segfaults on any reference to curses.keyname OR w.getkey()
the two are orthogonal paths to the failure)
There are still some things I don't understand about this.
The mouse stuff is completely opaque (and virtually undocumented).
> Bye
> Marco
> Experience is something you don't get until just after you need it.
^^^ the first time
More information about the Python-list
mailing list