概述
pipinstall colorama
# or
condainstall -c anaconda colorama
pycharm中的颜色显示:
初始化
fromcolorama import init,deinit,reinit
init()
deinit()
reinit()
教程
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('backto normal now')
print('\033[31m'+ 'some red text')
print('\033[39m')# and reset to default color
from colorama import init
from termcolor import colored
# useColorama to make Termcolor work on Windows too
init()
# then useTermcolor for all colored text output
print(colored('Hello,World!', 'green', 'on_red'))
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
示例1
import os
from colorama import init, Fore, Back, Style
if "PYCHARM_HOSTED" in os.environ:
convert = False
strip = False
else:
convert = None
strip = None
init(convert=convert, strip=strip)
print(Fore.GREEN + 'green, '
+ Fore.RED + 'red, '
+ Fore.RESET + 'normal, '
, end='')
print(Back.GREEN + 'green, '
+ Back.RED + 'red, '
+ Back.RESET + 'normal, '
, end='')
print(Style.DIM + 'dim, '
+ Style.BRIGHT + 'bright, '
+ Style.NORMAL + 'normal'
, end=' ')
print()
示例2
import os
from colorama import init, Fore, Back, Style
if "PYCHARM_HOSTED" in os.environ:
convert = False
strip = False
else:
convert = None
strip = None
init(convert=convert, strip=strip)
print(Fore.CYAN + Back.MAGENTA + Style.BRIGHT + 'Line 1: colored, with autoreset=True')
print(Fore.YELLOW + Back.BLUE + Style.BRIGHT + 'Line 2: colored, with autoreset=False')
示例3
import os
import sys
from colorama import init, Fore, Back, Style, AnsiToWin32
if "PYCHARM_HOSTED" in os.environ:
convert = False
strip = False
else:
convert = None
strip = None
init(convert=convert, strip=strip)
print('%sWrapped yellow going to stdout, via the default print function.' % Fore.YELLOW)
print('%sUnwrapped CYAN, using the file parameter to write via colorama the AnsiToWin32 function.' % Fore.CYAN, file=AnsiToWin32(sys.stdout))
示例4
import os
import colorama
from colorama import init, Fore, Back, Style, Cursor,colorama_text
from random import randint, choice
from string import printable
if "PYCHARM_HOSTED" in os.environ:
convert = False
strip = False
else:
convert = None
strip = None
init(convert=convert, strip=strip)
"""automatically reset stdout"""
with colorama_text():
print(Fore.GREEN + 'text is green')
print(Fore.RESET + 'text is back to normal')
pycharm支持colorama
import os
from colorama import init, Fore, Back, Style
if "PYCHARM_HOSTED" in os.environ:
convert = False
strip = False
else:
convert = None
strip = None
init(convert=convert, strip=strip)