简体   繁体   中英

Using Python curses colors, I don't understand what is wrong with my code

I want to use Python curses library to make the fisrt draft to a new app. I have the basics of curses, but don't know why the example below writes the text in gray instead of red :

import curses
from curses import wrapper
from time import sleep

def main(stdscr):
    curses.noecho()
    curses.cbreak()
    curses.start_color()
    stdscr.keypad(True)
    stdscr.addstr(10,10,"This text should be red",curses.COLOR_RED)
    stdscr.refresh()
    sleep(2)
    curses.nocbreak()
    stdscr.keypad(False)
    curses.echo()
    curses.endwin()

wrapper(main)

I know I shouldn't rewrite the initialization steps after using "wrapper", but that's not the issue, I think, the text is still gray without it. I run this app in an xterm window, "has_colors" tells me the term is color capable (which I know because "ls" is colorfull).

If anyone could explain to me what I am doing wrong, I would be delighted :-P

Thanks for any help.

The attribute should use a color pair , not a color number . You would make a color pair using init_pair , use it viacolor_pair

For example

import curses
from curses import wrapper
from time import sleep

def main(stdscr):
    curses.noecho()
    curses.cbreak()
    curses.start_color()
    stdscr.keypad(True)
    curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLUE)
    stdscr.addstr(10,10,"This text should be red",curses.color_pair(1))
    stdscr.refresh()
    sleep(2)
    curses.nocbreak()
    stdscr.keypad(False)
    curses.echo()
    curses.endwin()

wrapper(main)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM