Python:如何使用单击.sech

2024-09-30 05:27:14 发布

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

我用的是点击模块。在

pip install click

这给了我红色的文本

^{pr2}$

现在我希望只有“Error:”显示为红色。如何使用click.secho来完成此操作?在


Tags: 模块installpip文本errorclick红色pr2
3条回答

您可以使用内置的secho命令和nl(新行)标志。在

在您的特定用例中,它看起来像

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...")

来自click文档中的echo method

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

因此,组合时,您应该有如下内容

^{pr2}$

为了得到你想要的东西。在

相关问题 更多 >

    热门问题