欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python終端輸出彩色字符方法詳解

 更新時(shí)間:2020年02月11日 15:45:03   作者:qhh0205  
這篇文章主要介紹了Python終端輸出彩色字符方法詳解,需要的朋友可以參考下

有時(shí)候需要在終端顯示彩色的字符,即根據(jù)需要顯示不同顏色的字符串,比如我們要在終端打印一行錯誤提示信息,要把它弄成紅色的。其實(shí)這個在Python中很好實(shí)現(xiàn),使用轉(zhuǎn)義序列來實(shí)現(xiàn)不同顏色的顯示,轉(zhuǎn)義序列以ESC開頭,它的ASCII碼八進(jìn)制為 \033。顯示格式為:\033[顯示方式;前景色;背景色m

用這種原生的轉(zhuǎn)義序列輸出,在linux下完全支持,但是在windows下確存在兼容問題,比如在win10下可以正常顯示顏色,在win7下確不支持。因此可以使用python標(biāo)準(zhǔn)庫提供的colorama模塊

輸出彩色字體,這個模塊是跨平臺的,內(nèi)部實(shí)現(xiàn)也是采用轉(zhuǎn)義序列來顯示顏色的,只不過對windows平臺做了特殊處理,因此完全兼容linux和windows各個版本。

以下封裝了一個Colored類,提供了兩個版本,第一個版本采用原生的轉(zhuǎn)義字符序列輸出各種顏。

第二個版本用python標(biāo)準(zhǔn)庫的colorama模塊兼容windows和linux。當(dāng)要在終端打印彩色字體時(shí)直接調(diào)用對應(yīng)的方法即可,很方便。

一.Colored版本

1:采用原生的轉(zhuǎn)義字符序列---對windows有的版本不支持(比如win7),linux完美支持

#coding:gbk
# ------------------------------------------------
#  python終端顯示彩色字符類,可以調(diào)用不同的方法
# 選擇不同的顏色.使用方法看示例代碼就很容易明白.
# ------------------------------------------------
#
# 顯示格式: \033[顯示方式;前景色;背景色m
# ------------------------------------------------
# 顯示方式       說明
#  0         終端默認(rèn)設(shè)置
#  1         高亮顯示
#  4         使用下劃線
#  5         閃爍
#  7         反白顯示
#  8         不可見
#  22        非粗體
#  24        非下劃線
#  25        非閃爍
#
#  前景色       背景色      顏色
#   30        40       黑色
#   31        41       紅色
#   32        42       綠色
#   33        43       黃色
#   34        44       藍(lán)色
#   35        45       紫紅色
#   36        46       青藍(lán)色
#   37        47       白色
# ------------------------------------------------
class Colored(object):
  # 顯示格式: \033[顯示方式;前景色;背景色m
  # 只寫一個字段表示前景色,背景色默認(rèn)
  RED = '\033[31m'    # 紅色
  GREEN = '\033[32m'   # 綠色
  YELLOW = '\033[33m'  # 黃色
  BLUE = '\033[34m'   # 藍(lán)色
  FUCHSIA = '\033[35m'  # 紫紅色
  CYAN = '\033[36m'   # 青藍(lán)色
  WHITE = '\033[37m'   # 白色
 
  #: no color
  RESET = '\033[0m'   # 終端默認(rèn)顏色
 
  def color_str(self, color, s):
    return '{}{}{}'.format(
      getattr(self, color),
      s,
      self.RESET
    )
 
  def red(self, s):
    return self.color_str('RED', s)
 
  def green(self, s):
    return self.color_str('GREEN', s)
 
  def yellow(self, s):
    return self.color_str('YELLOW', s)
 
  def blue(self, s):
    return self.color_str('BLUE', s)
 
  def fuchsia(self, s):
    return self.color_str('FUCHSIA', s)
 
  def cyan(self, s):
    return self.color_str('CYAN', s)
 
  def white(self, s):
    return self.color_str('WHITE', s)
 
# ----------使用示例如下:-------------
color = Colored()
print color.red('I am red!')
print color.green('I am gree!')
print color.yellow('I am yellow!')
print color.blue('I am blue!')
print color.fuchsia('I am fuchsia!')
print color.cyan('I am cyan!')
print color.white('I am white')

顏色對比圖(根據(jù)需要自己設(shè)置對應(yīng)的值):

運(yùn)行效果:

二.Colored版本

2:采用python標(biāo)準(zhǔn)庫的colorama模塊--兼容linux和windows各個版本:

# -----------------colorama模塊的一些常量---------------------------
# Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
# Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
# Style: DIM, NORMAL, BRIGHT, RESET_ALL
#
 
from colorama import init, Fore, Back, Style
init(autoreset=True)
class Colored(object):
 
  # 前景色:紅色 背景色:默認(rèn)
  def red(self, s):
    return Fore.RED + s + Fore.RESET
 
  # 前景色:綠色 背景色:默認(rèn)
  def green(self, s):
    return Fore.GREEN + s + Fore.RESET
 
  # 前景色:黃色 背景色:默認(rèn)
  def yellow(self, s):
    return Fore.YELLOW + s + Fore.RESET
 
  # 前景色:藍(lán)色 背景色:默認(rèn)
  def blue(self, s):
    return Fore.BLUE + s + Fore.RESET
 
  # 前景色:洋紅色 背景色:默認(rèn)
  def magenta(self, s):
    return Fore.MAGENTA + s + Fore.RESET
 
  # 前景色:青色 背景色:默認(rèn)
  def cyan(self, s):
    return Fore.CYAN + s + Fore.RESET
 
  # 前景色:白色 背景色:默認(rèn)
  def white(self, s):
    return Fore.WHITE + s + Fore.RESET
 
  # 前景色:黑色 背景色:默認(rèn)
  def black(self, s):
    return Fore.BLACK
 
  # 前景色:白色 背景色:綠色
  def white_green(self, s):
    return Fore.WHITE + Back.GREEN + s + Fore.RESET + Back.RESET
 
color = Colored()
print color.red('I am red!')
print color.green('I am gree!')
print color.yellow('I am yellow!')
print color.blue('I am blue!')
print color.magenta('I am magenta!')
print color.cyan('I am cyan!')
print color.white('I am white!')
print color.white_green('I am white green!')

運(yùn)行效果:

更多關(guān)于Python終端輸出彩色字符方法請查看下面的相關(guān)鏈接

相關(guān)文章

最新評論