代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
实现Windows CMD命令行彩色输出
作者:
yanyahu
/ 发布于
2014/9/10
/
856
用Python写命令行程序的时候,单一的输出颜色太单调。其实我们可以加些色彩,比如用红色表示警告,绿色表示结果正常等。网上也有几篇类似的帖子,但是没有把问题讲清楚,贴的代码也不是太清晰。这里,对Windows下CMD输出彩色进行完整的讲解和易用的代码函数使用展示。 原理解析参见:http://my.oschina.net/bluefly/blog/311209
#! /usr/bin/env python #coding=utf-8 import ctypes,sys STD_INPUT_HANDLE = -10 STD_OUTPUT_HANDLE = -11 STD_ERROR_HANDLE = -12 # 字体颜色定义 ,关键在于颜色编码,由2位十六进制组成,分别取0~f,前一位指的是背景色,后一位指的是字体色 #由于该函数的限制,应该是只有这16种,可以前景色与背景色组合。也可以几种颜色通过或运算组合,组合后还是在这16种颜色中 # Windows CMD命令行 字体颜色定义 text colors FOREGROUND_BLACK = 0x00 # black. FOREGROUND_DARKBLUE = 0x01 # dark blue. FOREGROUND_DARKGREEN = 0x02 # dark green. FOREGROUND_DARKSKYBLUE = 0x03 # dark skyblue. FOREGROUND_DARKRED = 0x04 # dark red. FOREGROUND_DARKPINK = 0x05 # dark pink. FOREGROUND_DARKYELLOW = 0x06 # dark yellow. FOREGROUND_DARKWHITE = 0x07 # dark white. FOREGROUND_DARKGRAY = 0x08 # dark gray. FOREGROUND_BLUE = 0x09 # blue. FOREGROUND_GREEN = 0x0a # green. FOREGROUND_SKYBLUE = 0x0b # skyblue. FOREGROUND_RED = 0x0c # red. FOREGROUND_PINK = 0x0d # pink. FOREGROUND_YELLOW = 0x0e # yellow. FOREGROUND_WHITE = 0x0f # white. # Windows CMD命令行 背景颜色定义 background colors BACKGROUND_BLUE = 0x10 # dark blue. BACKGROUND_GREEN = 0x20 # dark green. BACKGROUND_DARKSKYBLUE = 0x30 # dark skyblue. BACKGROUND_DARKRED = 0x40 # dark red. BACKGROUND_DARKPINK = 0x50 # dark pink. BACKGROUND_DARKYELLOW = 0x60 # dark yellow. BACKGROUND_DARKWHITE = 0x70 # dark white. BACKGROUND_DARKGRAY = 0x80 # dark gray. BACKGROUND_BLUE = 0x90 # blue. BACKGROUND_GREEN = 0xa0 # green. BACKGROUND_SKYBLUE = 0xb0 # skyblue. BACKGROUND_RED = 0xc0 # red. BACKGROUND_PINK = 0xd0 # pink. BACKGROUND_YELLOW = 0xe0 # yellow. BACKGROUND_WHITE = 0xf0 # white. # get handle std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) def set_cmd_text_color(color, handle=std_out_handle): Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color) return Bool #reset white def resetColor(): set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) ############################################################### #暗蓝色 #dark blue def printDarkBlue(mess): set_cmd_text_color(FOREGROUND_DARKBLUE) sys.stdout.write(mess) resetColor() #暗绿色 #dark green def printDarkGreen(mess): set_cmd_text_color(FOREGROUND_DARKGREEN) sys.stdout.write(mess) resetColor() #暗天蓝色 #dark sky blue def printDarkSkyBlue(mess): set_cmd_text_color(FOREGROUND_DARKSKYBLUE) sys.stdout.write(mess) resetColor() #暗红色 #dark red def printDarkRed(mess): set_cmd_text_color(FOREGROUND_DARKRED) sys.stdout.write(mess) resetColor() #暗粉红色 #dark pink def printDarkPink(mess): set_cmd_text_color(FOREGROUND_DARKPINK) sys.stdout.write(mess) resetColor() #暗黄色 #dark yellow def printDarkYellow(mess): set_cmd_text_color(FOREGROUND_DARKYELLOW) sys.stdout.write(mess) resetColor() #暗白色 #dark white def printDarkWhite(mess): set_cmd_text_color(FOREGROUND_DARKWHITE) sys.stdout.write(mess) resetColor() #暗灰色 #dark gray def printDarkGray(mess): set_cmd_text_color(FOREGROUND_DARKGRAY) sys.stdout.write(mess) resetColor() #蓝色 #blue def printBlue(mess): set_cmd_text_color(FOREGROUND_BLUE) sys.stdout.write(mess) resetColor() #绿色 #green def printGreen(mess): set_cmd_text_color(FOREGROUND_GREEN) sys.stdout.write(mess) resetColor() #天蓝色 #sky blue def printSkyBlue(mess): set_cmd_text_color(FOREGROUND_SKYBLUE) sys.stdout.write(mess) resetColor() #红色 #red def printRed(mess): set_cmd_text_color(FOREGROUND_RED) sys.stdout.write(mess) resetColor() #粉红色 #pink def printPink(mess): set_cmd_text_color(FOREGROUND_PINK) sys.stdout.write(mess) resetColor() #黄色 #yellow def printYellow(mess): set_cmd_text_color(FOREGROUND_YELLOW) sys.stdout.write(mess) resetColor() #白色 #white def printWhite(mess): set_cmd_text_color(FOREGROUND_WHITE) sys.stdout.write(mess) resetColor() ################################################## #白底黑字 #white bkground and black text def printWhiteBlack(mess): set_cmd_text_color(FOREGROUND_BLACK | BACKGROUND_WHITE) sys.stdout.write(mess) resetColor() #白底黑字 #white bkground and black text def printWhiteBlack_2(mess): set_cmd_text_color(0xf0) sys.stdout.write(mess) resetColor() #黄底蓝字 #white bkground and black text def printYellowRed(mess): set_cmd_text_color(BACKGROUND_YELLOW | FOREGROUND_RED) sys.stdout.write(mess) resetColor() ############################################################## if __name__ == '__main__': print printDarkBlue(u'printDarkBlue:暗蓝色文字\n'.encode('gb2312')) printDarkGreen(u'printDarkGreen:暗绿色文字\n'.encode('gb2312')) printDarkSkyBlue(u'printDarkSkyBlue:暗天蓝色文字\n'.encode('gb2312')) printDarkRed(u'printDarkRed:暗红色文字\n'.encode('gb2312')) printDarkPink(u'printDarkPink:暗粉红色文字\n'.encode('gb2312')) printDarkYellow(u'printDarkYellow:暗黄色文字\n'.encode('gb2312')) printDarkWhite(u'printDarkWhite:暗白色文字\n'.encode('gb2312')) printDarkGray(u'printDarkGray:暗灰色文字\n'.encode('gb2312')) printBlue(u'printBlue:蓝色文字\n'.encode('gb2312')) printGreen(u'printGreen:绿色文字\n'.encode('gb2312')) printSkyBlue(u'printSkyBlue:天蓝色文字\n'.encode('gb2312')) printRed(u'printRed:红色文字\n'.encode('gb2312')) printPink(u'printPink:粉红色文字\n'.encode('gb2312')) printYellow(u'printYellow:黄色文字\n'.encode('gb2312')) printWhite(u'printWhite:白色文字\n'.encode('gb2312')) printWhiteBlack(u'printWhiteBlack:白底黑字输出\n'.encode('gb2312')) printWhiteBlack_2(u'printWhiteBlack_2:白底黑字输出(直接传入16进制参数)\n'.encode('gb2312')) printYellowRed(u'printYellowRed:黄底红字输出\n'.encode('gb2312'))
试试其它关键字
彩色输出
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
yanyahu
贡献的其它代码
(
12
)
.
整数的划分数
.
最优装船问题
.
四皇后(回溯法)
.
数字的全排列
.
寻找假币
.
约瑟夫环
.
回文字符串判定
.
任意长度整数加法
.
0-1背包问题
.
实现Windows CMD命令行彩色输出
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3