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抓取网页图片示例(python爬虫)
Apr 27 Python
从零学python系列之教你如何根据图片生成字符画
May 23 Python
Python中的测试模块unittest和doctest的使用教程
Apr 14 Python
Python实现对字符串的加密解密方法示例
Apr 29 Python
Python实现进程同步和通信的方法
Jan 02 Python
python程序运行进程、使用时间、剩余时间显示功能的实现代码
Jul 11 Python
Python 200行代码实现一个滑动验证码过程详解
Jul 11 Python
python实现Pyecharts实现动态地图(Map、Geo)
Mar 25 Python
matlab 计算灰度图像的一阶矩,二阶矩,三阶矩实例
Apr 22 Python
Jupyter Notebook 实现正常显示中文和负号
Apr 24 Python
详解python tcp编程
Aug 24 Python
Python自动化工具之实现Excel转Markdown表格
Apr 08 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
全国FM电台频率大全 - 27 陕西省
2020/03/11 无线电
php下的权限算法的实现
2007/04/28 PHP
php学习 函数 课件
2008/06/15 PHP
让CodeIgniter数据库缓存自动过期的处理的方法
2014/06/12 PHP
zend framework中使用memcache的方法
2016/03/04 PHP
jQuery 选择器、DOM操作、事件、动画
2010/11/25 Javascript
js获取input标签的输入值实现代码
2013/08/05 Javascript
jQuery使用ajax跨域获取数据的简单实例
2016/05/18 Javascript
深入理解MVC中的时间js格式化
2016/05/19 Javascript
KnockoutJS 3.X API 第四章之数据控制流foreach绑定
2016/10/10 Javascript
Angular-Touch库用法示例
2016/12/22 Javascript
使用vue根据状态添加列表数据和删除列表数据的实例
2018/09/29 Javascript
vue+Vue Router多级侧导航切换路由(页面)的实现代码
2018/12/20 Javascript
no-vnc和node.js实现web远程桌面的完整步骤
2019/08/11 Javascript
JavaScript使用canvas绘制随机验证码
2020/02/17 Javascript
node.js +mongdb实现登录功能
2020/06/18 Javascript
js实现弹窗效果
2020/08/09 Javascript
原生js生成图片验证码
2020/10/11 Javascript
Python中用于计算对数的log()方法
2015/05/15 Python
Python使用BeautifulSoup库解析HTML基本使用教程
2016/03/31 Python
python3操作微信itchat实现发送图片
2018/02/24 Python
python爬虫之模拟登陆csdn的实例代码
2018/05/18 Python
使用NumPy和pandas对CSV文件进行写操作的实例
2018/06/14 Python
python实现维吉尼亚加密法
2019/03/20 Python
浅谈pytorch中的BN层的注意事项
2020/06/23 Python
CSS3中的注音对齐属性ruby-align用法指南
2016/07/01 HTML / CSS
美国最大的烧烤架和户外生活用品专业零售商:Barbeques Galore
2021/01/09 全球购物
教师自我评价范例
2013/09/24 职场文书
护理专业大学生自我推荐信
2014/01/25 职场文书
机电一体化毕业生自荐信
2014/06/19 职场文书
二年级学生期末评语
2014/12/26 职场文书
清洁工个人总结
2015/03/04 职场文书
出国留学自荐信模板
2015/03/06 职场文书
深入理解Pytorch微调torchvision模型
2021/11/11 Python
win11怎么用快捷键锁屏? windows11锁屏的几种方法
2021/11/21 数码科技
Mysql分析设计表主键为何不用uuid
2022/03/31 MySQL