3

我想做什么?

我正在使用rich图书馆以不同的颜色打印单词。

我想出了以下程序来做到这一点:

from rich import print as rprint

rprint('[[green]1[/green]] Create new password')
print('[2] See existing passwords')
print('[3] Exit')

输出:

在此处输入图像描述

我的问题

正如您在上图中所见,1与下方的方括号相比,其周围的方括号颜色更亮2 & 3。有没有办法让方括号都是相同的颜色(灰色)而不是白色?

提前致谢。

笔记:

我知道这不会妨碍程序的工作方式,但我喜欢美观的东西,这真的让我很烦。

此外,我只是在测试如何使用 更改颜色rich,但我愿意接受有关其他方式的建议。

4

2 回答 2

1

由于操作对其他方式开放,这是我的做法..

使用标准终端颜色代码启动一个类。

class bcolors:

    ResetAll = "\033[0m"

    Bold       = "\033[1m"
    Dim        = "\033[2m"
    Underlined = "\033[4m"
    Blink      = "\033[5m"
    Reverse    = "\033[7m"
    Hidden     = "\033[8m"

    ResetBold       = "\033[21m"
    ResetDim        = "\033[22m"
    ResetUnderlined = "\033[24m"
    ResetBlink      = "\033[25m"
    ResetReverse    = "\033[27m"
    ResetHidden     = "\033[28m"

    Default      = "\033[39m"
    Black        = "\033[30m"
    Red          = "\033[31m"
    Green        = "\033[32m"
    Yellow       = "\033[33m"
    Blue         = "\033[34m"
    Magenta      = "\033[35m"
    Cyan         = "\033[36m"
    LightGray    = "\033[37m"
    DarkGray     = "\033[90m"
    LightRed     = "\033[91m"
    LightGreen   = "\033[92m"
    LightYellow  = "\033[93m"
    LightBlue    = "\033[94m"
    LightMagenta = "\033[95m"
    LightCyan    = "\033[96m"
    White        = "\033[97m"

    BackgroundDefault      = "\033[49m"
    BackgroundBlack        = "\033[40m"
    BackgroundRed          = "\033[41m"
    BackgroundGreen        = "\033[42m"
    BackgroundYellow       = "\033[43m"
    BackgroundBlue         = "\033[44m"
    BackgroundMagenta      = "\033[45m"
    BackgroundCyan         = "\033[46m"
    BackgroundLightGray    = "\033[47m"
    BackgroundDarkGray     = "\033[100m"
    BackgroundLightRed     = "\033[101m"
    BackgroundLightGreen   = "\033[102m"
    BackgroundLightYellow  = "\033[103m"
    BackgroundLightBlue    = "\033[104m"
    BackgroundLightMagenta = "\033[105m"
    BackgroundLightCyan    = "\033[106m"
    BackgroundWhite        = "\033[107m"

你的程序

print(f"[{bcolors.Green}1{bcolors.ResetAll}] Create new password")
print('[2] See existing passwords')
print('[3] Exit')

输出:

在此处输入图像描述

于 2021-03-02T12:45:48.290 回答
1

Rich 在输出上执行突出显示,用于数字、字符串、数据等。在您的示例中,它突出显示大括号,这在您打印数据结构时会很有帮助。

如果您构造一个 Console 对象并highlight=False在 print 方法上设置,则可以禁用此功能。

这是一个例子:

from rich.console import Console
console = Console()
console.print('[[green]1[/green]] Create new password', highlight=False)

有关详细信息,请参阅突出显示的文档。

于 2021-03-02T13:47:37.200 回答