gpt4 book ai didi

python - [31m 文本而不是颜色的输出

转载 作者:太空狗 更新时间:2023-10-29 17:47:36 26 4
gpt4 key购买 nike

我正在尝试使用 colorama 打印彩色文本,但是当我编译一个 exe 并运行以下...

from colorama import Fore, Back, Style
print(Fore.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')
I get output of::

输出:

[31mtext
[0m
back to normal now

编译为 pyinstaller exe 时是否可以打印颜色,或者这根本不可能?

最佳答案

在 Windows 上,您必须使用 colorama.init() 初始化 Colorama(见第二行):

from colorama import Fore, Back, Style
colorama.init()
print(Fore.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')

我已经在 cmdPowerShell 中测试了这段代码,它产生了预期的彩色输出。

来自 Colorama docs :

On Windows, calling init() will filter ANSI escape sequences out of any text sent to stdout or stderr, and replace them with equivalent Win32 calls.

On other platforms, calling init() has no effect (unless you request other optional functionality; see “Init Keyword Args”, below). By design, this permits applications to call init() unconditionally on all platforms, after which ANSI output should just work.

关于python - [31m 文本而不是颜色的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47432418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com