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安装第三方库的3种方法
Jun 21 Python
PyQt5实现拖放功能
Apr 25 Python
python字符串常用方法
Jun 14 Python
pycharm 在windows上编辑代码用linux执行配置的方法
Oct 27 Python
python 使用re.search()筛选后 选取部分结果的方法
Nov 28 Python
浅析python3字符串格式化format()函数的简单用法
Dec 07 Python
python实现大学人员管理系统
Oct 25 Python
Python GUI库PyQt5图形和特效样式QSS介绍
Feb 25 Python
基于Python共轭梯度法与最速下降法之间的对比
Apr 02 Python
利用python生成照片墙的示例代码
Apr 09 Python
python安装后的目录在哪里
Jun 21 Python
Django利用AJAX技术实现博文实时搜索
May 06 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
一段php加密解密的代码
2007/07/16 PHP
PHP判断一个gif图片是否为动态图片的方法
2014/11/19 PHP
ThinkPHP实现ajax仿官网搜索功能实例
2014/12/02 PHP
PHP测试框架PHPUnit组织测试操作示例
2018/05/28 PHP
PHP中的访问修饰符简单比较
2019/02/02 PHP
Yii框架 session 数据库存储操作方法示例
2019/11/18 PHP
thinkphp5实现微信扫码支付
2019/12/23 PHP
JCalendar 日历控件 v1.0 beta[兼容IE&Firefox] 有文档和例子
2007/05/30 Javascript
Javascript 定时器调用传递参数的方法
2009/11/12 Javascript
jquery.ajax的url中传递中文乱码问题的解决方法
2014/02/07 Javascript
深入探讨JavaScript、JQuery屏蔽网页鼠标右键菜单及禁止选择复制
2014/06/10 Javascript
使用不同的方法结合/合并两个JS数组
2014/09/18 Javascript
jQuery带箭头提示框tooltips插件集锦
2014/11/17 Javascript
JavaScript中reduce()方法的使用详解
2015/06/09 Javascript
js精美的幻灯片画集特效代码分享
2015/08/29 Javascript
shiro授权的实现原理
2017/09/21 Javascript
vue-music关于Player播放器组件详解
2017/11/28 Javascript
vuex state及mapState的基础用法详解
2018/04/19 Javascript
推荐一个基于Node.js的表单验证库
2019/02/15 Javascript
Angular6使用forRoot() 注册单一实例服务问题
2019/08/27 Javascript
layui自定义验证,用ajax查询后台是否有重复数据,form.verify的例子
2019/09/06 Javascript
解决vue项目中遇到 Cannot find module ‘chalk‘ 报错的问题
2020/11/05 Javascript
python操作xml文件示例
2014/04/07 Python
Python中的rjust()方法使用详解
2015/05/19 Python
实例解析Python的Twisted框架中Deferred对象的用法
2016/05/25 Python
Python实现将文本生成二维码的方法示例
2017/07/18 Python
Python3.4 tkinter,PIL图片转换
2018/06/21 Python
Python实现使用request模块下载图片demo示例
2019/05/24 Python
解决Django no such table: django_session的问题
2020/04/07 Python
python输出国际象棋棋盘的实例分享
2020/11/26 Python
小学教师办公室制度
2014/02/03 职场文书
入党个人总结范文
2015/03/02 职场文书
2015-2016年小学教导工作总结
2015/07/21 职场文书
你对自己的信用报告有过了解吗?
2019/07/09 职场文书
oracle索引总结
2021/09/25 Oracle
MYSQL中文乱码问题的解决方案
2022/06/14 MySQL