首页 > 解决方案 > colorama/termcolor 不返回彩色线条

问题描述

from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

输出

【31msome红色文字

[42mand 有绿色背景

[2mand 在昏暗的文本中

[0米

现在恢复正常

我使用 colorama 库来更改几个单词的字体颜色。不幸的是,它没有返回任何带有各自颜色的行。任何人都可以帮我确定问题吗?提前致谢

标签: colorscolorama

解决方案


您的代码可以在我的 Linux 机器上运行,但如果您使用的是 Windows,则必须先调用init()(根据文档)。代码如下所示:

#!/usr/bin/python3
from colorama import Fore, Back, Style, init

# only required on Windows
init()

# print some example lines
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

推荐阅读