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

 更新时间:2017年05月02日 15:58:52   作者:Luan  
这篇文章主要介绍了在Windows和Linux中Python输出彩色文字的方法,通过设置彩色文字给大家更醒目的效果,文中给出了详细的介绍和示例代码,需要的朋友可以参考借鉴,下面来一起看看吧。

前言

最近在项目中需要输出彩色的文字来提醒用户,以前写过,但是只能在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:

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:

[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多线程实现同时执行两个while循环的操作

    python多线程实现同时执行两个while循环的操作

    这篇文章主要介绍了python多线程实现同时执行两个while循环的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • Python使用jsonpath-rw模块处理Json对象操作示例

    Python使用jsonpath-rw模块处理Json对象操作示例

    这篇文章主要介绍了Python使用jsonpath-rw模块处理Json对象操作,结合实例形式分析了Python使用requests与response处理json的方法,并给出了jsonpath_rw模块操作json对象的基本示例,需要的朋友可以参考下
    2018-07-07
  • python 含子图的gif生成时内存溢出的方法

    python 含子图的gif生成时内存溢出的方法

    今天小编就为大家分享一篇python 含子图的gif生成时内存溢出的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • python实现通过flask和前端进行数据收发

    python实现通过flask和前端进行数据收发

    今天小编就为大家分享一篇python实现通过flask和前端进行数据收发,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Jupyter Notebook 安装配置与使用详解

    Jupyter Notebook 安装配置与使用详解

    这篇文章主要介绍了Jupyter Notebook 安装配置与使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • python用字典统计单词或汉字词个数示例

    python用字典统计单词或汉字词个数示例

    这篇文章主要介绍了python用字典统计单词或汉字词个数示例,需要的朋友可以参考下
    2014-04-04
  • Python超有用的多版本管理工具pyenv

    Python超有用的多版本管理工具pyenv

    使用不同的Python版本,如果我们把需要的不同版本的Python都下载到服务器上,管理起来会非常困难,多版本并存又容易互相干扰,接下来就来介绍一个Python环境管理工具:pyenv,就可轻松的在多个版本的Python之间自由切换,需要的朋友可以参考下
    2021-09-09
  • 详解python-opencv 常用函数

    详解python-opencv 常用函数

    这篇文章主要介绍了python-opencv 常用函数,主要包括读取图像保存图像和缩放图像的相关知识,需要的朋友可以参考下
    2022-08-08
  • django 自定义用户user模型的三种方法

    django 自定义用户user模型的三种方法

    这篇文章主要介绍了django 自定义用户user模型的三种方法,需要的朋友可以参考下
    2014-11-11
  • Python解决非线性规划中经济调度问题

    Python解决非线性规划中经济调度问题

    Scipy是Python算法库和数学工具包,包括最优化、线性代数、积分、插值、特殊函数、傅里叶变换等模块。scipy.optimize模块中提供了多个用于非线性规划问题的方法,适用于不同类型的问题。本文将利用起解决经济调度问题,感兴趣的可以了解一下
    2022-05-05

最新评论