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

colorama · PyPI

		
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是针对字体格式

set_cols_align(self, array) 设置水平对齐

set_cols_valign(self, array) 设置垂直对齐

			
from texttable import Texttable

table = Texttable()
table.set_cols_align(["l", "r", "c"])
table.set_cols_valign(["t", "m", "b"])
table.add_rows([["Name", "Age", "Nickname"],
                ["Mr\nXavier\nHuon", 32, "Xav'"],
                ["Mr\nBaptiste\nClement", 1, "Baby"],
                ["Mme\nLouise\nBourgeau", 28, "Lou\n\nLoue"]])
print(table.draw())
print()				
			
			

输出结果

			
+----------+-----+----------+
|   Name   | Age | Nickname |
+==========+=====+==========+
| Mr       |     |          |
| Xavier   |  32 |          |
| Huon     |     |   Xav'   |
+----------+-----+----------+
| Mr       |     |          |
| Baptiste |   1 |          |
| Clement  |     |   Baby   |
+----------+-----+----------+
| Mme      |     |   Lou    |
| Louise   |  28 |          |
| Bourgeau |     |   Loue   |
+----------+-----+----------+			
			
			

3.3.2. 设置表格风格

			
from texttable import Texttable
table = Texttable()
for header in (Texttable.BORDER, Texttable.HEADER, Texttable.HLINES, Texttable.VLINES):
    table.set_deco(header)

    table.set_cols_align(["l", "r", "c"])
    table.set_cols_valign(["t", "m", "b"])
    table.add_rows([["Name", "Age", "Nickname"],
                    ["Neo", 35, "netkiller"],
                    ["李磊", 23, "Lee"],
                    ["韩美美", 28, "May"]])
    print(table.draw())
    print("\n\n")			
			
			

输出结果

						
+--------------------------+
|  Name    Age   Nickname  |
| Neo       35   netkiller |
| 李磊      23      Lee    |
| 韩美美    28      May    |
+--------------------------+



 Name    Age   Nickname 
========================
Neo       35   netkiller
李磊      23      Lee   
韩美美    28      May   
Neo       35   netkiller
李磊      23      Lee   
韩美美    28      May   



 Name    Age   Nickname 
Neo       35   netkiller
+--------------------------+
李磊      23      Lee   
+--------------------------+
韩美美    28      May   
+--------------------------+
Neo       35   netkiller
+--------------------------+
李磊      23      Lee   
+--------------------------+
韩美美    28      May   
+--------------------------+
Neo       35   netkiller
+--------------------------+
李磊      23      Lee   
+--------------------------+
韩美美    28      May   



 Name  | Age | Nickname 
Neo    |  35 | netkiller
李磊   |  23 |    Lee   
韩美美 |  28 |    May   
Neo    |  35 | netkiller
李磊   |  23 |    Lee   
韩美美 |  28 |    May   
Neo    |  35 | netkiller
李磊   |  23 |    Lee   
韩美美 |  28 |    May   
Neo    |  35 | netkiller
李磊   |  23 |    Lee   
韩美美 |  28 |    May   			
			
			

3.3.3. 自定义风格

自定义行列线条字符

			
set_chars(self, array)
     |      Set the characters used to draw line
  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

netkiller-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值