输出[31m文本而不是颜色 [英] output of [31m text instead of color

查看:263
本文介绍了输出[31m文本而不是颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to print coloured text with colorama but when I compile an exe and run following...

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时是否可以打印颜色?或者这根本不可能吗?

Is it possible to print colors when compiling to pyinstaller exe or is this simply not possible?

推荐答案

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

On Windows, you have to initialize Colorama with colorama.init() (see the second line):

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中测试了此代码,并产生了预期的彩色输出.

I have tested this code in cmd and PowerShell and it produces the expected colored output.

来自科罗拉多州文档:

在Windows上,调用init()将从所有发送到stdoutstderr的文本中过滤出ANSI转义序列,并将其替换为等效的Win32调用.

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.

在其他平台上,调用init()无效(除非您请求其他可选功能;请参见下面的"Init Keyword Args").根据设计,这允许应用程序在所有平台上无条件调用init(),之后ANSI输出应该可以正常工作.

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.

这篇关于输出[31m文本而不是颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆