from colorama import init as colorama_init from colorama import Fore from colorama import Style
colorama_init()
print(f"This is {Fore.GREEN}color{Style.RESET_ALL}!")
4. 使用sty库
sty库支持8位和24位(RGB)颜色,允许自定义样式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from sty import fg, bg, ef, rs
foo = fg.red + 'This is red text!' + fg.rs bar = bg.blue + 'This has a blue background!' + bg.rs baz = ef.italic + 'This is italic text' + rs.italic qux = fg(201) + 'This is pink text using 8bit colors' + fg.rs qui = fg(255, 10, 10) + 'This is red text using 24bit colors.' + fg.rs
print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)
# 使用termcolor模块 from termcolor import colored print(colored('hello', 'red'), colored('world', 'green'))
# 使用Colorama库 from colorama import init as colorama_init from colorama import Fore from colorama import Style colorama_init() print(f"This is {Fore.GREEN}color{Style.RESET_ALL}!")
# 使用sty库 from sty import fg, bg, ef, rs foo = fg.red + 'This is red text!' + fg.rs bar = bg.blue + 'This has a blue background!' + bg.rs baz = ef.italic + 'This is italic text' + rs.italic qux = fg(201) + 'This is pink text using 8bit colors' + fg.rs qui = fg(255, 10, 10) + 'This is red text using 24bit colors.' + fg.rs from sty import Style, RgbFg fg.orange = Style(RgbFg(255, 150, 50)) buf = fg.orange + 'Yay, Im orange.' + fg.rs print(foo, bar, baz, qux, qui, buf, sep='\n')
# 使用Rich库 from rich importprint print("[red]Color[/] in the [bold magenta]Terminal[/]!")