如何在Python中将彩色文本输出到终端?
当前回答
我已经将joeld的答案打包到一个模块中,该模块具有全局函数,我可以在代码中的任何地方使用这些函数。
文件:log.py
def enable():
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = "\033[1m"
def disable():
HEADER = ''
OKBLUE = ''
OKGREEN = ''
WARNING = ''
FAIL = ''
ENDC = ''
def infog(msg):
print(OKGREEN + msg + ENDC)
def info(msg):
print(OKBLUE + msg + ENDC)
def warn(msg):
print(WARNING + msg + ENDC)
def err(msg):
print(FAIL + msg + ENDC)
enable()
使用方法如下:
import log
log.info("Hello, World!")
log.err("System Error")
其他回答
您可以使用curses库的Python实现:curses-字符单元显示的终端处理
此外,运行此程序,您将找到您的盒子:
for i in range(255):
print i, chr(i)
还有Python termcolor模块。用法很简单:
from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')
或者在Python 3中:
print(colored('hello', 'red'), colored('world', 'green'))
然而,对于游戏编程和你想做的“彩色方块”来说,它可能不够复杂。。。
要使ANSI代码在windows上运行,请首先运行
os.system('color')
我建议这个新图书馆Printy。他们刚刚发布了版本1.2.0作为跨平台库。
过来看:GitHub上的Printy
它基于标志,所以你可以做类似的事情
from printy import printy
# With global flags, this will apply a bold (B) red (r) color and an underline (U) to the whole text
printy("Hello, World!", "rBU")
# With inline formats, this will apply a dim (D)
#blue (b) to the word 'Hello' and a stroken (S)
#yellow (y) to the word 'world', and the rest will remain as the predefined format
printy("this is a [bD]Hello@ [yS]world@ text")
我编写了一个在Linux、OS X和Windows中处理颜色的模块。它支持所有平台上的所有16种颜色,您可以在不同的时间设置前景和背景颜色,字符串对象可以为len()和.capitalize()等对象提供正常的结果。
https://github.com/Robpol86/colorclass
我有一个叫做colorist的图书馆。这非常简单。
以下是一些示例:
from colorit import *
# Use this to ensure that ColorIt will be usable by certain command line interfaces
# Note: This clears the terminal
init_colorit()
# Foreground
print(color("This text is red", Colors.red))
print(color("This text is orange", Colors.orange))
print(color("This text is yellow", Colors.yellow))
print(color("This text is green", Colors.green))
print(color("This text is blue", Colors.blue))
print(color("This text is purple", Colors.purple))
print(color("This text is white", Colors.white))
# Background
print(background("This text has a background that is red", Colors.red))
print(background("This text has a background that is orange", Colors.orange))
print(background("This text has a background that is yellow", Colors.yellow))
print(background("This text has a background that is green", Colors.green))
print(background("This text has a background that is blue", Colors.blue))
print(background("This text has a background that is purple", Colors.purple))
print(background("This text has a background that is white", Colors.white))
# Custom
print(color("This color has a custom grey text color", (150, 150, 150)))
print(background("This color has a custom grey background", (150, 150, 150)))
# Combination
print(
background(
color("This text is blue with a white background", Colors.blue), Colors.white
)
)
# If you are using Windows Command Line, this is so that it doesn't close immediately
input()
这为您提供了:
值得注意的是,这是跨平台的,已经在Mac、Linux和Windows上进行了测试。
你可能想试试:https://github.com/SuperMaZingCoder/colorit
coloret现在可以与PyPi一起安装了!您可以在Windows上使用pipinstall-color-it安装它,在macOS和Linux上使用pip3 install-color-it安装它。