Answer a question

Using curses in python you can easily use the default color scheme for the terminal using:

curses.use_default_colors()

However once you try to recolor any character, using a color pair you have to declare a background color:

curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)

I really don't want to change the background from the default but I would like to change the foreground.

Is there any way to either get the default background color? or to change just the foreground color?

I am aware that I could use ANSI escape codes to adjust just the foreground color, however ANSI codes are not compatible with curses and I would rather work with curses than rewrite everything in ANSI codes.

Answers

Ok, I figured it out,

If you call init_pair with -1 as a value it will fill in the terminal default. For example to make red text with the default background:

curses.init_pair(1, curses.COLOR_RED, -1)

Now curses.color_pair(1) will be set to the background. This will even work if you change the terminal's default while the program is running.

You do have to call curses.use_default_colors() first to use this.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐