python - Python 使用 Curses 显示所有可用颜色对时出现问题

标签 python python-3.x ncurses curses python-curses

我正在运行 Python 3.10,并且遇到了诅咒问题,特别是在尝试利用全部颜色对时

curses.COLORS 返回 256

curses.COLOR_PAIRS 返回 65536

curses.has_extended_color_support() 返回True

但是当我初始化 256、512、768、1024 等颜色对时...该颜色对似乎重置了初始化的颜色

此外,如果我尝试显示任何大于 255 的颜色对 (curses.COLORS - 1),它会使用小于 255 的颜色对之一,而不是我设置的颜色对 ID。 Curses 允许我使用这些数字运行 init_pair,但它完全忽略它们,而是使用颜色对 < 255

这是一个示例代码片段

#!/usr/bin/env python3.10

"""
Python3.10 added extended color support to curses
curses.COLOR_PAIRS (xterm-256color) returns 65,536
curses.COLORS (xterm-256color) returns 256
"""

import curses


def cycler(start_color, end_color):
    # cycle through all possible color IDs or pair IDs curses can display, loops indefinitely
    while True:
        for i in range(start_color, end_color):
            yield i


def main(stdscr):
    curses.start_color()
    height, width = stdscr.getmaxyx()

    RED = 1  # Reserve the red color at color ID 1
    curses.init_color(RED, 1000, 0, 0)

    color_cycler = cycler(2, curses.COLORS - 1)  # RED is reserved at 1, start at 2
    pair_cycler = cycler(1, curses.COLOR_PAIRS - 1)

    has_extended_support = curses.has_extended_color_support()
    stdscr.addstr(0, 0, f"COLORS:{curses.COLORS} "
                        f"COLOR_PAIRS:{curses.COLOR_PAIRS} "
                        f"ExtendedSupport:{has_extended_support}", curses.color_pair(1))

    for y in range(1, height - 1):
        for x in range(0, width, 5):

            color_id = next(color_cycler)
            pair_id = next(pair_cycler)

            intensity = int((color_id / (curses.COLORS - 1)) * 1000)
            curses.init_color(color_id, intensity, intensity, intensity)

            # create a new color pair, (RED) foreground, (color) background
            curses.init_pair(pair_id, RED, color_id)
            string = str(pair_id).rjust(5, ' ')
            stdscr.addstr(y, x, string, curses.color_pair(pair_id))

    stdscr.noutrefresh()
    curses.doupdate()
    stdscr.getch()


if __name__ == "__main__":
    curses.wrapper(main)

这是它为我生成的屏幕截图(注意数字 256、512、768、1024) 数字代表颜色对 ID

enter image description here

我尝试过的终端应用程序:

  • gnome 终端
  • 术语
  • 蒂尔达

都有同样的问题

最佳答案

您必须使用扩展接口(interface),如 ncurses 6.1 的发布公告中所述。 :

The motivation for making this extension came from noticing that termcap applications could (though not realistically) use larger numbers than would fit in 16-bits, and the fact that the number of color pairs for a 256-color xterm could not be expressed in terminfo (i.e., 32767 versus 65536). Also, a few terminals support direct-colors, which could use the extension.

Python 可能不会这样做(它的绑定(bind)仅覆盖 ncurses 的一部分),因此颜色值和颜色对是一个带符号的 16 位数字:

0..32767

现在... python 3.10 发布于 October 2021 。与颜色对相关的最新 ncurses 修复位于 August 2021 。如果您使用旧版本的 ncurses(例如,通过在 very old 上编译 python,例如 RHEL8),那么这只是一个已知的错误,答案将是“升级”。

关于python - Python 使用 Curses 显示所有可用颜色对时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74186123/

相关文章:

python - 使用 BeautifulSoup 获取日期

python - 有没有办法覆盖 nn.Module 上的向后操作

python - 零碎的 : no module named win32api (windows)

python - 类 python 中的 IndentationError

c - C编程中包含ncurses.h是否也包含stdio.h?

python - 套接字服务器的客户端不会抛出错误,表明尝试对非套接字的内容进行操作

python - 使用 pyinstaller 生成 .exe 后出现错误(没有名为 exchangelib 的模块)

python - Pandas - 从行中提取文本

linux - Raspbian 从源代码构建 ncurses 和 vim

python - 如何在 Python 的 curses 中获得更多颜色?