colorama是一个python专门用来在控制台、命令行输出彩色文字的模块,可以跨平台使用。

1. 安装colorama模块

pip install colorama

 

可用格式常数:

Fore:  BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Back:  BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Style: DIM, NORMAL, BRIGHT, RESET_ALL



"from colorama import Fore,Back,Style" 后使用Fore、Back、Style的常数+文本来使用:
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!!")

 

初始化并设置是否自动恢复

init(autoreset = True):
init(autoreset = False):

 

# 如果未设置autoreset=True,需要使用如下代码重置终端颜色为初始设置
#print(Fore.RESET + Back.RESET + Style.RESET_ALL)

 

实列如下:

python colorama 模块

 

 效果:

python colorama 模块

 

 

若要想让第二行颜色不是之前设置的红色就需要这样操作:


python colorama 模块

 

 效果:

python colorama 模块

 

相关文章:

  • 2021-08-14
  • 2022-01-20
  • 2021-05-16
  • 2021-08-26
  • 2021-06-29
  • 2021-07-09
  • 2022-02-09
  • 2022-03-02
猜你喜欢
  • 2021-10-09
  • 2021-09-12
  • 2021-06-04
  • 2022-12-23
  • 2021-10-08
相关资源
相似解决方案