标签:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # author:zml 4 5 def Colors(text, fcolor=None,bcolor=None,style=None): 6 ‘‘‘ 7 自定义字体样式及颜色 8 ‘‘‘ 9 # 字体颜色 10 fg={ 11 ‘black‘: ‘\033[30m‘, #字体黑 12 ‘red‘: ‘\033[31m‘, #字体红 13 ‘green‘: ‘\033[32m‘, #字体绿 14 ‘yellow‘: ‘\033[33m‘, #字体黄 15 ‘blue‘: ‘\033[34m‘, #字体蓝 16 ‘magenta‘: ‘\033[35m‘, #字体紫 17 ‘cyan‘: ‘\033[36m‘, #字体青 18 ‘white‘:‘\033[37m‘, #字体白 19 ‘end‘:‘\033[0m‘ #默认色 20 } 21 # 背景颜色 22 bg={ 23 ‘black‘: ‘\033[40m‘, #背景黑 24 ‘red‘: ‘\033[41m‘, #背景红 25 ‘green‘: ‘\033[42m‘, #背景绿 26 ‘yellow‘: ‘\033[43m‘, #背景黄 27 ‘blue‘: ‘\033[44m‘, #背景蓝 28 ‘magenta‘: ‘\033[45m‘, #背景紫 29 ‘cyan‘: ‘\033[46m‘, #背景青 30 ‘white‘:‘\033[47m‘, #背景白 31 } 32 # 内容样式 33 st={ 34 ‘bold‘: ‘\033[1m‘, #高亮 35 ‘url‘: ‘\033[4m‘, #下划线 36 ‘blink‘: ‘\033[5m‘, #闪烁 37 ‘seleted‘: ‘\033[7m‘, #反显 38 } 39 40 if fcolor in fg: 41 text=fg[fcolor]+text+fg[‘end‘] 42 if bcolor in bg: 43 text = bg[bcolor] + text + fg[‘end‘] 44 if style in st: 45 text = st[style] + text + fg[‘end‘] 46 return text
from color import Colors
print(Colors(‘文本内容‘,‘字体颜色‘,‘背景颜色‘,‘字体样式‘))
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
可以使用python的termcolor模块,简单快捷。避免重复造轮子
from termcolor import colored
print colored(‘hello‘, ‘red‘), colored(‘world‘, ‘green‘)
标签:
原文地址:http://www.cnblogs.com/zhanmeiliang/p/5948947.html