python在windows命令行下输出彩色文字的方法


Posted in Python onMarch 19, 2015

本文实例讲述了python在windows命令行下输出彩色文字的方法。分享给大家供大家参考。具体分析如下:

默认情况下python在控制台输出的文字信息都是黑白的,如果能将文字做成彩色的输出,输出效果会更完美,也很酷,不是吗,下面是一段演示代码,这段代码封装了一个color类用来输出带颜色的文字,只要调用该类里面的相关方法就可以了,非常简单。

#!/usr/bin/env python 

#encoding: utf-8

import ctypes

STD_INPUT_HANDLE = -10

STD_OUTPUT_HANDLE= -11

STD_ERROR_HANDLE = -12

FOREGROUND_BLACK = 0x0

FOREGROUND_BLUE = 0x01 # text color contains blue.

FOREGROUND_GREEN= 0x02 # text color contains green.

FOREGROUND_RED = 0x04 # text color contains red.

FOREGROUND_INTENSITY = 0x08 # text color is intensified.

BACKGROUND_BLUE = 0x10 # background color contains blue.

BACKGROUND_GREEN= 0x20 # background color contains green.

BACKGROUND_RED = 0x40 # background color contains red.

BACKGROUND_INTENSITY = 0x80 # background color is intensified.

class Color:

    ''' See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/windows_api_reference.asp

    for information on Windows APIs. - 3water.com'''

    std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

    def set_cmd_color(self, color, handle=std_out_handle):

        """(color) -> bit

        Example: set_cmd_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)

        """

        bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)

        return bool

    def reset_color(self):

        self.set_cmd_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)

    def print_red_text(self, print_text):

        self.set_cmd_color(FOREGROUND_RED | FOREGROUND_INTENSITY)

        print print_text

        self.reset_color()

    def print_green_text(self, print_text):

        self.set_cmd_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY)

        print print_text

        self.reset_color()

    def print_blue_text(self, print_text):

        self.set_cmd_color(FOREGROUND_BLUE | FOREGROUND_INTENSITY)

        print print_text

        self.reset_color()

    def print_red_text_with_blue_bg(self, print_text):

        self.set_cmd_color(FOREGROUND_RED | FOREGROUND_INTENSITY| BACKGROUND_BLUE | BACKGROUND_INTENSITY)

        print print_text

        self.reset_color()   

if __name__ == "__main__":

    clr = Color()

    clr.print_red_text('red')

    clr.print_green_text('green')

    clr.print_blue_text('blue')

    clr.print_red_text_with_blue_bg('background')

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python提取网页中超链接的方法
Sep 18 Python
Python实现简单的四则运算计算器
Nov 02 Python
Python实现的简单dns查询功能示例
May 24 Python
在Python 2.7即将停止支持时,我们为你带来了一份python 3.x迁移指南
Jan 30 Python
python微信跳一跳系列之棋子定位像素遍历
Feb 26 Python
Numpy掩码式数组详解
Apr 17 Python
python+selenium实现自动化百度搜索关键词
Jun 03 Python
PyQt5实现让QScrollArea支持鼠标拖动的操作方法
Jun 19 Python
Python学习笔记之错误和异常及访问错误消息详解
Aug 08 Python
Python selenium 自动化脚本打包成一个exe文件(推荐)
Jan 14 Python
python里glob模块知识点总结
Jan 05 Python
python如何用matplotlib创建三维图表
Jan 26 Python
python通过colorama模块在控制台输出彩色文字的方法
Mar 19 #Python
python实现颜色rgb和hex相互转换的函数
Mar 19 #Python
python实现从一组颜色中找出与给定颜色最接近颜色的方法
Mar 19 #Python
python遍历类中所有成员的方法
Mar 18 #Python
python获取图片颜色信息的方法
Mar 18 #Python
Python调用C语言开发的共享库方法实例
Mar 18 #Python
Python两个整数相除得到浮点数值的方法
Mar 18 #Python
You might like
德劲1102收音机的打理维修案例
2021/03/02 无线电
PHP调用Twitter的RSS的实现代码
2010/03/10 PHP
Yii使用CLinkPager分页实例详解
2014/07/23 PHP
jquery json 实例代码
2010/12/02 Javascript
图片上传插件jquery.uploadify详解
2013/11/15 Javascript
原生js实现淘宝首页点击按钮缓慢回到顶部效果
2014/04/06 Javascript
js继承call()和apply()方法总结
2014/12/08 Javascript
使用基于Node.js的构建工具Grunt来发布ASP.NET MVC项目
2016/02/15 Javascript
BootstrapValidator不触发校验的实现代码
2016/09/28 Javascript
javascript 数据存储的常用函数总结
2017/06/01 Javascript
基于jquery实现多级菜单效果
2017/07/25 jQuery
关于在mongoose中填充外键的方法详解
2017/08/14 Javascript
for循环 + setTimeout 结合一些示例(前端面试题)
2017/08/30 Javascript
vue中实现在外部调用methods的方法(推荐)
2018/02/08 Javascript
Angular6笔记之封装http的示例代码
2018/07/27 Javascript
vue中v-for循环给标签属性赋值的方法
2018/10/18 Javascript
原生JavaScript实现的无缝滚动功能详解
2020/01/17 Javascript
基于JavaScript实现随机点名器
2021/02/25 Javascript
[01:06]DOTA2小知识课堂 Ep.02 吹风竟可解梦境缠绕
2019/12/05 DOTA
python 解析html之BeautifulSoup
2009/07/07 Python
巧用Python装饰器 免去调用父类构造函数的麻烦
2012/05/18 Python
Python 模拟员工信息数据库操作的实例
2017/10/23 Python
Python基于动态规划算法解决01背包问题实例
2017/12/06 Python
pytorch之添加BN的实现
2020/01/06 Python
Python控制台实现交互式环境执行
2020/06/09 Python
python字典与json转换的方法总结
2020/12/28 Python
Muziker英国:中欧最大的音乐家商店
2020/02/05 全球购物
英国最大的天然和有机产品在线零售商之一:Big Green Smile
2020/05/06 全球购物
实习教师个人的自我评价
2013/11/08 职场文书
一年级学生评语大全
2014/04/21 职场文书
群众路线领导对照材料
2014/08/23 职场文书
教师节活动总结
2014/08/29 职场文书
学习心得体会
2019/06/20 职场文书
Python合并多张图片成PDF
2021/06/09 Python
TV动画《八十龟酱观察日记》第四季宣传PV公布
2022/04/06 日漫
Java8 Stream API 提供了一种高效且易于使用的处理数据的方式
2022/04/13 Java/Android