是否可以使用python中的curses获取默认的背景颜色? [英] Is it possible to get the default background color using curses in python?

查看:127
本文介绍了是否可以使用python中的curses获取默认的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用python中的curses,您可以轻松地使用以下默认终端颜色方案:

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?

我知道我可以使用ANSI转义码来仅调整前景色,但是ANSI代码与curses不兼容,宁愿使用curses而不是用ANSI代码重写所有内容。

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.

推荐答案

好,我知道了,

如果使用 -1 作为值调用 init_pair ,它将填写终端默认值。例如,使红色文本具有默认背景:

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)

现在 curses.color_pair(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.

您必须调用 curses.use_default_colors()首先使用它。

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

这篇关于是否可以使用python中的curses获取默认的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆