Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

第 3 章 终端环境开发

目录

3.1. ANSI Color
3.1.1. ansicolors
3.1.2. termcolor
3.1.3. Colorama
3.2. 进度条
3.2.1. progress
3.2.2. tqdm
3.2.3. alive-progress
3.3. texttable - module for creating simple ASCII tables
3.3.1. 对齐设置
3.3.2. 设置表格风格
3.3.3. 自定义风格
3.3.4. 设置列数据类型
3.3.5. 彩色表格
3.4. prompt_toolkit
3.4.1. 安装
3.5. Simple Terminal Menu
3.6. picotui
3.7. TUI
3.7.1. Console
3.7.2. urwid
3.7.3. pycdk
3.7.4. python-newt - A NEWT module for Python

3.1. ANSI Color

3.1.1. ansicolors

3.1.2. termcolor

		
from termcolor import colored

# then use Termcolor for all colored text output
print(colored('Hello, World!', 'green', 'on_red'))		
		
			
		
import sys
from termcolor import colored, cprint

text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
cprint('Hello, World!', 'green', 'on_red')

print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
print_red_on_cyan('Hello, World!')
print_red_on_cyan('Hello, Universe!')

for i in range(10):
    cprint(i, 'magenta', end=' ')

cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)		
		
			

3.1.3. Colorama

https://pypi.org/project/colorama/

		
pip install colorama
		
			

3.1.3.1. 初始化操作

init(autoreset = False),当 autoreset = True 时自动恢复到默认颜色

		
#!/usr/bin/env python 
from colorama import init, Fore, Back, Style

if __name__ == "__main__":
 
    init(autoreset=True)    #  初始化,自动恢复到默认颜色
    print(Fore.RED + 'some red text')
    print(Back.GREEN + 'and with a green background')
    print(Style.DIM + 'and in dim text')		
		
				

3.1.3.2. 常用格式

Fore是针对字体颜色,Back是针对字体背景颜色,Style是针对字体格式

  • 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