1. Color definition Description
format: \033[display mode; foreground color; background colour m
Foreground background color
---------------------------------------
30 40 Black
31 41 Red
32 42 Green
33 43 Yellow
34 44 Blue
35 45 Purplish red
36 46 Cyan Blue
37 47 White
Meaning of display mode
-------------------------
0 Terminal default settings
1 Highlighting
4 using Underscores
5 Flashing
7 anti-white display
8 not visible
Example:
\033[1;31;40m <!--1-highlight 31-foreground color Red 40-background black--
\033[0m <!--use terminal default settings, that is, color settings-->] ]Description of the 2.ANSI control code
\33[0m Close All Properties
\33[1m setting High brightness
\33[4m underline
\33[5m Flashing
\33[7m Reverse Display
\33[8m blanking
\33[30m--\33[37m setting foreground color
\33[40m--\33[47m set background color
\33[na the cursor to move n rows
\33[NB cursor down n rows
\33[NC cursor Right shifts n rows
\33[nd cursor left n rows
\33[y;xh Setting the cursor position
\33[2j Clear Screen
\33[k clears the contents from the cursor to the end of the line
\33[s Save cursor position
\33[u Restore cursor position
\33[?25l Hide Cursor
\33[?25h Display Cursor3. Custom color functions
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #author:zml4 5 defColors (text, fcolor=none,bcolor=none,style=None):6 " "7 custom font styles and colors8 " "9 #Font ColorTenfg={ One 'Black':'\033[30m',#Font Black A 'Red':'\033[31m',#Font Red - 'Green':'\033[32m',#Font Green - 'Yellow':'\033[33m',#Font Yellow the 'Blue':'\033[34m',#Font Blue - 'Magenta':'\033[35m',#Font Violet - 'Cyan':'\033[36m',#Font Green - ' White':'\033[37m',#Font White + 'End':'\033[0m' #Default Color - } + #Background Color Abg={ at 'Black':'\033[40m',#Black Background - 'Red':'\033[41m',#Red Background - 'Green':'\033[42m',#Green Background - 'Yellow':'\033[43m',#Yellow background - 'Blue':'\033[44m',#Blue background - 'Magenta':'\033[45m',#Purple Background in 'Cyan':'\033[46m',#Green Background - ' White':'\033[47m',#White background to } + #Content Style -st={ the 'Bold':'\033[1m',#Highlight * 'URL':'\033[4m',#Underline $ 'Blink':'\033[5m',#flashingPanax Notoginseng 'seleted':'\033[7m',#Reverse Display - } the + ifFcolorinchFG: Atext=fg[fcolor]+text+fg['End'] the ifBcolorinchBG: +Text = Bg[bcolor] + text + fg['End'] - ifStyleinchST: $Text = St[style] + text + fg['End'] $ returnText
3.1 How to use
From color import Colors
Print (Colors (' text content ', ' Font color ', ' background color ', ' font style ')
Reference:
Http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
http://blog.csdn.net/gatieme/article/details/45439671
Https://taizilongxu.gitbooks.io/stackoverflow-about-python/content/30/README.html
Http://www.361way.com/python-color/4596.html
Summarize:
You can use Python's Termcolor module, which is quick and easy. Avoid making wheels repeatedly
From Termcolor Import colored
Print colored (' Hello ', ' red '), colored (' world ', ' green ')
Python Terminal color settings