Windows和Linux下Python输出彩色文字的方法教程


Posted in Python onMay 02, 2017

前言

最近在项目中需要输出彩色的文字来提醒用户,以前写过,但是只能在win上面运行。

今天搜了下看有没有在win和Linux上通用的输出彩色文字的模块,结果发现没有,,于是就自己弄了一个,分享下,以后用的时候翻翻博客,方便别人也方便自己。

win下输出彩色文字,网上有两种方法一种是用system执行命令来设置颜色,感觉还是不太好,用ctypes模块实现更好点。

linux下设置颜色,网上只找到了一种方法,下面不废话了,直接贴下代码:

示例代码

import platform
if 'Windows' in platform.system():
 import sys
 import ctypes
 __stdInputHandle = -10
 __stdOutputHandle = -11
 __stdErrorHandle = -12
 __foreGroundBLUE = 0x09
 __foreGroundGREEN = 0x0a
 __foreGroundRED = 0x0c
 __foreGroundYELLOW = 0x0e
 stdOutHandle=ctypes.windll.kernel32.GetStdHandle(__stdOutputHandle)
 def setCmdColor(color,handle=stdOutHandle):
 return ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
 def resetCmdColor():
 setCmdColor(__foreGroundRED | __foreGroundGREEN | __foreGroundBLUE)
 def printBlue(msg):
 setCmdColor(__foreGroundBLUE)
 sys.stdout.write(msg + '\n')
 resetCmdColor()
 def printGreen(msg):
 setCmdColor(__foreGroundGREEN)
 sys.stdout.write(msg + '\n')
 resetCmdColor()
 def printRed(msg):
 setCmdColor(__foreGroundRED)
 sys.stdout.write(msg + '\n')
 resetCmdColor()
 def printYellow(msg):
 setCmdColor(__foreGroundYELLOW)
 sys.stdout.write(msg + '\n')
 resetCmdColor()
else:
 STYLE = {
 'fore':{
 'red': 31,
 'green': 32,
 'yellow': 33,
 'blue': 34,
 }
 }
 def UseStyle(msg, mode = '', fore = '', back = '40'):
 fore = '%s' % STYLE['fore'][fore] if STYLE['fore'].has_key(fore) else ''
 style = ';'.join([s for s in [mode, fore, back] if s])
 style = '\033[%sm' % style if style else ''
 end = '\033[%sm' % 0 if style else ''
 return '%s%s%s' % (style, msg, end)
 def printRed(msg):
 print UseStyle(msg,fore='red')
 def printGreen(msg):
 print UseStyle(msg,fore='green')
 def printYellow(msg):
 print UseStyle(msg,fore='yellow')
 def printBlue(msg):
 print UseStyle(msg,fore='blue')

效果图:

Windows:

Windows和Linux下Python输出彩色文字的方法教程

C:\luan\lu4n.com-sqli>python
Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from color import *
>>> printRed('Red')
Red
>>> printGreen('Green')
Green
>>> printYellow('Yellow')
Yellow
>>> printBlue('Blue')
Blue
>>> print 'http://lu4n.com/'
http://lu4n.com/
>>>

Linux:

Windows和Linux下Python输出彩色文字的方法教程

[root@Luan ~]# nano test_color.py
[root@Luan ~]# python
Python 2.7.5 (default, Jun 17 2014, 18:11:42) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test_color import *
>>> printRed('Red')
Red
>>> printGreen('Green')
Green
>>>

用起来很容易,直接from color import *就可以用了,有4种常用颜色可以使用,分别写了4个函数:

提示信息 printBlue

成功信息 printGreen

失败信息 printRed

警告信息 printYellow

和bootstrap的几种颜色差不多,应该够用了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
python在windows下实现备份程序实例
Jul 04 Python
Django中使用locals()函数的技巧
Jul 16 Python
详解Python中映射类型(字典)操作符的概念和使用
Aug 19 Python
Python 模板引擎的注入问题分析
Jan 01 Python
Django中针对基于类的视图添加csrf_exempt实例代码
Feb 11 Python
Python 中的Selenium异常处理实例代码
May 03 Python
tensorflow实现加载mnist数据集
Sep 08 Python
python查询文件夹下excel的sheet名代码实例
Apr 02 Python
python实现比较类的两个instance(对象)是否相等的方法分析
Jun 26 Python
python 使用socket传输图片视频等文件的实现方式
Aug 07 Python
python pyenv多版本管理工具的使用
Dec 23 Python
Python中openpyxl实现vlookup函数的实例
Oct 28 Python
python中字符串类型json操作的注意事项
May 02 #Python
python实现逻辑回归的方法示例
May 02 #Python
pycharm中连接mysql数据库的步骤详解
May 02 #Python
Python多线程实现同步的四种方式
May 02 #Python
Python之Web框架Django项目搭建全过程
May 02 #Python
python3实现抓取网页资源的 N 种方法
May 02 #Python
Pycharm学习教程(2) 代码风格
May 02 #Python
You might like
《忧国的莫里亚蒂》先导宣传图与STAFF公开
2020/03/04 日漫
刚才在简化php的库,结果发现很多东西
2006/12/31 PHP
php使用curl发送json格式数据实例
2013/12/17 PHP
PHP中使用php://input处理相同name值的表单数据
2015/02/03 PHP
基于CakePHP实现的简单博客系统实例
2015/06/28 PHP
php表单习惯用的正则表达式
2017/10/11 PHP
网页自动刷新,不产生嗒嗒声的一个解决方法
2007/03/27 Javascript
JavaScript学习笔记之获取当前目录的实现代码
2010/12/14 Javascript
intro.js 页面引导简单用法 分享
2013/08/06 Javascript
jquery实现从数组移除指定的值
2015/06/24 Javascript
纯js实现的积木(div层)拖动功能示例
2017/07/19 Javascript
Angular 开发学习之Angular CLI的安装使用
2017/12/31 Javascript
Webpack的dll功能使用
2018/06/28 Javascript
一次Webpack配置文件的分离实战记录
2018/11/30 Javascript
Vue源码中要const _toStr = Object.prototype.toString的原因分析
2018/12/09 Javascript
vue返回上一页面时回到原先滚动的位置的方法
2018/12/20 Javascript
vue设置导航栏、侧边栏为公共页面的例子
2019/11/01 Javascript
node创建Vue项目步骤详解
2020/03/06 Javascript
js基于canvas实现时钟组件
2021/02/07 Javascript
提升Python程序运行效率的6个方法
2015/03/31 Python
Python及PyCharm下载与安装教程
2017/11/18 Python
NumPy 基本切片和索引的具体使用方法
2019/04/24 Python
python matplotlib拟合直线的实现
2019/11/19 Python
Python小程序之在图片上加入数字的代码
2019/11/26 Python
纽约现代艺术博物馆商店:MoMA STORE(室内家具和杂货商品)
2016/08/02 全球购物
挪威户外活动服装和装备购物网站:Bergfreunde挪威
2016/10/20 全球购物
Tiqets英国:智能手机上的文化和娱乐门票
2019/07/10 全球购物
俄罗斯大型在线书店:Читай-город
2019/10/10 全球购物
销售经理工作职责
2014/02/03 职场文书
高中教师先进事迹材料
2014/08/22 职场文书
党员教师个人对照检查材料(群众路线)
2014/09/26 职场文书
2015年项目经理工作总结
2015/04/30 职场文书
爱护环境建议书
2015/09/14 职场文书
军训决心书范文
2015/09/22 职场文书
2016年清明节网上祭英烈活动总结
2016/04/01 职场文书
SpringBoot 集成Redis 过程
2021/06/02 Redis