Python-更改IDLE文本颜色在Windows上不起作用

时间:2019-03-10 20:39:37

标签: python python-idle colorama

我正在使用colorama尝试在IDLE shell中进行模拟。这是我的代码:

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')

我的输出看起来像这样:

Error

什么是不正确的?为什么要在一开始打印那些奇怪的字母?我正在使用Windows OS

PS:我也尝试过在命令提示符中运行此命令,并且得到了类似的输出结果

2 个答案:

答案 0 :(得分:1)

您缺少call to init(向下滚动到“用法”):

from colorama import Fore, Back, Style, init

# Here
init()

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')

哪些输出,彩色

some red text
and with a green background
and in dim text

back to normal now

这在IDLE中仍然无效,但在cmdpowershell中有效。

答案 1 :(得分:0)

在Windows上,Colorama假定输出进入Windows文本控制台。命令提示符使用该控制台。从图标或“开始”菜单项启动时,python.exe也是如此。 Colorama发送ANSI转义码,并进行控制台可识别的win32调用。它不能直接用于其文本小部件的文本使用不同方法着色的图形框架。

相关问题