我刚刚在 Windows 上为 Python 2.7安装了termcolor 。当我尝试打印彩色文本时,我得到的是颜色代码。
from termcolor import colored
print colored('Text text text', 'red')
结果如下:
当我尝试将脚本作为独立应用程序运行时,我在 Far Manager 上获得了相同的结果。
我刚刚在 Windows 上为 Python 2.7安装了termcolor 。当我尝试打印彩色文本时,我得到的是颜色代码。
from termcolor import colored
print colored('Text text text', 'red')
结果如下:
当我尝试将脚本作为独立应用程序运行时,我在 Far Manager 上获得了相同的结果。
要使 termcolor 中使用的 ANSI 颜色与 Windows 终端一起使用,您还需要 import/init colorama
;
>>> from termcolor import *
>>> cprint('hello', 'red')
←[31mhello←[0m
>>> import colorama
>>> colorama.init()
>>> cprint('hello', 'red')
hello <-- in red color
>>>
windows 命令提示符使用命令更改终端输出颜色。您可以执行命令“颜色颜色代码”来立即更改颜色。只需使用命令color即可激活此颜色功能。
简而言之.. 要使您的脚本正常工作,请在脚本的开头运行它。
import os
os.system('color')
在 termcolor2 模块中,您必须输入:
import termcolor2
import colorama
colorama.init()
myText = input("Type a text : ")
color = input("What color you want? : ")
print(termcolor2.colored(myText, color))
而已...
有没有工作:
在导入 termcolor 之前插入:
import subprocess
subprocess.call('', shell=True)
没用:
无法解释它为什么起作用,只是我能够比较一个颜色正常工作的脚本和一个不能正常工作的脚本。
一个非常简单的解决方案是创建一个定义颜色的类并在函数中使用它,您不必导入任何模块只需复制并粘贴它:-
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def c_print(color, text):
if color == "green":
return print(bcolors.OKGREEN + text + bcolors.ENDC)
if color == "cyan":
return print(bcolors.OKCYAN + text + bcolors.ENDC)
if color == "blue":
return print(bcolors.OKBLUE + text + bcolors.ENDC)
line = f"{bcolors.OKCYAN}It will wish you on every start up{bcolors.ENDC}"
c_print("cyan", line)
在现代 Windows 系统上,控制台主机窗口应具有 ENABLE_VIRTUAL_TERMINAL_PROCESSING 模式以正确处理 ANSI 颜色序列。
没有外部依赖,纯 WinAPI(通过 ctypes):
def enable_vt_processing():
import ctypes
STD_OUTPUT_HANDLE = -11
handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
mode = ctypes.c_ulong()
ok = ctypes.windll.kernel32.GetConsoleMode(handle, ctypes.byref(mode))
assert ok
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
ok = ctypes.windll.kernel32.SetConsoleMode(handle, ctypes.c_ulong(mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING))
assert ok
添加这个以使 termcolor 在 Windows 中也能工作:
import colorama
colorama.init()
在 StackOverflow 上找到的所有问题和答案似乎都是专门针对 Windows 的,但我想补充一点,为 Linux 制作的应用程序也可能想要这样做。如果您需要从 Linux 命令行将程序的输出通过管道传输到一个新文件中(例如,python3 myprogram.py > output.txt
),您可能会在该文件中看到奇怪的控制字符!这通常是不可取的,尤其是在系统管理员正在使用的日志文件中。上面的代码片段在 Ubuntu 上为我解决了这个问题。
这是一个简单的函数,我发现它对彩色打印很有用。您无需进行任何导入,也无需记住复杂的 ANSI 代码。该函数使用标准 RGB 元组来定义前景色和背景色。您可以在https://www.google.com/search?q=rgb+color+picker&oq=rgb+color+picker&aqs=chrome 找到 RGB 颜色选择器。 .69i57j0l7.5967j0j8&sourceid=chrome&ie=UTF-8
def print_in_color(txt_msg,fore_tupple,back_tupple,):
#prints the text_msg in the foreground color specified by fore_tupple with the background specified by back_tupple
#text_msg is the text, fore_tupple is foregroud color tupple (r,g,b), back_tupple is background tupple (r,g,b)
rf,gf,bf=fore_tupple
rb,gb,bb=back_tupple
msg='{0}' + txt_msg
mat='\33[38;2;' + str(rf) +';' + str(gf) + ';' + str(bf) + ';48;2;' + str(rb) + ';' +str(gb) + ';' + str(bb) +'m'
print(msg .format(mat))
print('\33[0m') # returns default print color to back to black
# example of use using a message with variables
fore_color='cyan'
back_color='dark green'
msg='foreground color is {0} and the background color is {1}'.format(fore_color, back_color)
print_in_color(msg, (0,255,255),(0,127,127))