如何使用Click.secho在特定颜色中仅设置一个单词

2024-09-30 05:31:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用单击模块

pip install click

这给了我红色文本

import click
click.secho('Error: This error is ...xx', fg='red')

现在我只希望“Error:”显示为红色。如何使用click.secho实现这一点


Tags: 模块installpip文本importiserrorred
3条回答

来自echo methodclick文档

In addition to that, if colorama is installed, the echo function will also support clever handling of ANSI codes.

来自^{}文档

print('\033[31m' + 'some red text')
print('\033[30m') # and reset to default color

因此,结合起来,您应该有如下内容

click.echo('\033[31m' + 'Error:' + '\033[30m' + ' This error ... ')

为了得到你想要的东西

您可以使用带有nl(新行)标志的内置secho命令

在您的特定用例中

click.secho('Error', fg='red', nl=False) # This will prevent the secho statement from starting a new line
click.echo(': This error is ...xx')

这将为您提供所需的输出

click.echoclick.style一起使用

click.echo(click.style("Error", fg="red") + ": This error is...")

相关问题 更多 >

    热门问题