Termcolor包的使用

2019-10-05  本文已影响0人  csuhan

termcolor包是用来给命令行中文字添加颜色等效果的库,其使用十分简单。

  1. 安装
pip install termcolor
  1. 使用。其基本方法有:colored,cprint
    其中cprint相当于print(colored(something))
    两个函数的参数完全相同:cprint('sometext','color','on_color',attrs=[])
    其中color可选:
grey
red
green
yellow
blue
magenta
cyan
white

on_color即为背景色,可选:

on_grey
on_red
on_green
on_yellow
on_blue
on_magenta
on_cyan
on_white

attrs为其他参数,可组合使用,可选:

bold
dark
underline
blink
reverse
concealed
  1. 实例:
from termcolor import  colored,cprint

# print(colored(something)) equals cprint(something)
colored_text = colored("hello,wolrd",'cyan')
print(colored_text)

cprint("hello,wolrd",'cyan')

cprint("hello,world",'green','on_yellow',attrs=['bold','underline'])

# generated by http://asciiflow.com
demo_text = '''
+----------------------------------+
|                                  |
|                                  |
|                                  |
|       Mask R-CNN Test Tool       |
|                                  |
|                                  |
|                   author:csuhan  |
+----------------------------------+
'''
cprint(demo_text,'magenta',attrs=['bold','reverse'])

运行结果:


result
上一篇 下一篇

猜你喜欢

热点阅读