Python字体颜⾊设置
行有余力Python字体颜⾊设置
平时学习⼯作中,我们经常会接触到⼀些⼤佬写的Python⼯具,运⾏起来总会显⽰出五颜六⾊的字体,⽐如红⾊代表Error ,黄⾊代表Warning ,绿⾊代表Success 。⽽⾃⼰写出来的Python命令⾏代码,⼤多只有⼀种颜⾊。所以,要怎么样才能让⾃⼰的程序看起来更有逼格⼀些呢?
在这⾥,笔者就给⼤家分享⼀套个⼈认为最好⽤的Python字体库,⼤家在使⽤的时候只要import这个库就好了~~
Python字体库源码如下:
#! /usr/bin/env python
#coding=utf-8
guimiimport ctypes,sys
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12
# 字体颜⾊定义 ,关键在于颜⾊编码,由2位⼗六进制组成,分别取0~f,前⼀位指的是背景⾊,后⼀位指的是字体⾊
#由于该函数的限制,应该是只有这16种,可以前景⾊与背景⾊组合。也可以⼏种颜⾊通过或运算组合,组合后还是在这16种颜⾊中
# Windows CMD命令⾏字体颜⾊定义 text colors
FOREGROUND_BLACK = 0x00 # black.
FOREGROUND_DARKBLUE = 0x01 # dark blue.
FOREGROUND_DARKGREEN = 0x02 # dark green.
FOREGROUND_DARKSKYBLUE = 0x03 # dark skyblue.
FOREGROUND_DARKRED = 0x04 # dark red.
FOREGROUND_DARKPINK = 0x05 # dark pink.
FOREGROUND_DARKYELLOW = 0x06 # dark yellow.
FOREGROUND_DARKWHITE = 0x07 # dark white.
FOREGROUND_DARKGRAY = 0x08 # dark gray.
FOREGROUND_BLUE = 0x09 # blue.
FOREGROUND_GREEN = 0x0a # green.
FOREGROUND_SKYBLUE = 0x0b # skyblue.
FOREGROUND_RED = 0x0c # red.
FOREGROUND_PINK = 0x0d # pink.
FOREGROUND_YELLOW = 0x0e # yellow.
FOREGROUND_WHITE = 0x0f # white.
# Windows CMD命令⾏背景颜⾊定义 background colors
BACKGROUND_BLUE = 0x10 # dark blue.
BACKGROUND_GREEN = 0x20 # dark green.
BACKGROUND_DARKSKYBLUE = 0x30 # dark skyblue.
BACKGROUND_DARKRED = 0x40 # dark red.
BACKGROUND_DARKPINK = 0x50 # dark pink.
BACKGROUND_DARKYELLOW = 0x60 # dark yellow.
BACKGROUND_DARKWHITE = 0x70 # dark white.
BACKGROUND_DARKGRAY = 0x80 # dark gray.
形容美食诱人的句子BACKGROUND_BLUE = 0x90 # blue.
BACKGROUND_GREEN = 0xa0 # green.
BACKGROUND_SKYBLUE = 0xb0 # skyblue.
BACKGROUND_RED = 0xc0 # red.
BACKGROUND_PINK = 0xd0 # pink.
BACKGROUND_YELLOW = 0xe0 # yellow.
BACKGROUND_WHITE = 0xf0 # white.
# get handle
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
def t_cmd_text_color(color, handle=std_out_handle):
Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
return Bool
#ret white
def retColor():
t_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
>>>>>>>>>>>>####dark blue
惊艳的诗句def printDarkBlue(mess):
t_cmd_text_color(FOREGROUND_DARKBLUE)
sys.stdout.write(mess)
retColor()
#暗绿⾊
#dark green
def printDarkGreen(mess):
t_cmd_text_color(FOREGROUND_DARKGREEN) sys.stdout.write(mess)
retColor()
#暗天蓝⾊
#dark sky blue
def printDarkSkyBlue(mess):
t_cmd_text_color(FOREGROUND_DARKSKYBLUE) sys.stdout.write(mess)
retColor()
#暗红⾊
#dark red
def printDarkRed(mess):
t_cmd_text_color(FOREGROUND_DARKRED)
sys.stdout.write(mess)
retColor()
#暗粉红⾊
#dark pink
def printDarkPink(mess):
t_cmd_text_color(FOREGROUND_DARKPINK)
sys.stdout.write(mess)
retColor()
#暗黄⾊
#dark yellow
def printDarkYellow(mess):
决心书格式t_cmd_text_color(FOREGROUND_DARKYELLOW) sys.stdout.write(mess)
retColor()
#暗⽩⾊
#dark white
def printDarkWhite(mess):
t_cmd_text_color(FOREGROUND_DARKWHITE)
sys.stdout.write(mess)
retColor()
#暗灰⾊
#dark gray
def printDarkGray(mess):
t_cmd_text_color(FOREGROUND_DARKGRAY)
sys.stdout.write(mess)
retColor()
#蓝⾊
#blue
def printBlue(mess):
t_cmd_text_color(FOREGROUND_BLUE)
sys.stdout.write(mess)
retColor()
#绿⾊rve
#green
def printGreen(mess):
t_cmd_text_color(FOREGROUND_GREEN)
sys.stdout.write(mess)
retColor()
#天蓝⾊
#sky blue
def printSkyBlue(mess):
t_cmd_text_color(FOREGROUND_SKYBLUE)
sys.stdout.write(mess)
retColor()
#红⾊
#red
def printRed(mess):
t_cmd_text_color(FOREGROUND_RED)
sys.stdout.write(mess)
retColor()
#pink
def printPink(mess):
t_cmd_text_color(FOREGROUND_PINK)
sys.stdout.write(mess)
retColor()
#黄⾊
#yellow
def printYellow(mess):
t_cmd_text_color(FOREGROUND_YELLOW)
sys.stdout.write(mess)
retColor()
#⽩⾊
#white
def printWhite(mess):
t_cmd_text_color(FOREGROUND_WHITE)
sys.stdout.write(mess)
retColor()
>>>>>>>>>>#⽩底⿊字
#white bkground and black text
def printWhiteBlack(mess):
t_cmd_text_color(FOREGROUND_BLACK | BACKGROUND_WHITE) sys.stdout.write(mess)
retColor()
#⽩底⿊字
#white bkground and black text
def printWhiteBlack_2(mess):
t_cmd_text_color(0xf0)
sys.stdout.write(mess)
retColor()
独奏曲#黄底蓝字
#white bkground and black text
def printYellowRed(mess):
t_cmd_text_color(BACKGROUND_YELLOW | FOREGROUND_RED) sys.stdout.write(mess)
retColor()
字体库⽰例代码:
#! /usr/bin/env python
#coding=utf-8
from cmd_color_printers import *
printDarkBlue("Hello\n")
printDarkGreen("Hello\n")
printDarkSkyBlue("Hello\n")
printDarkRed("Hello\n")
printDarkPink("Hello\n")
printDarkYellow("Hello\n")
printDarkWhite("Hello\n")
printDarkGray("Hello\n")
山科大printBlue("Hello\n")
printGreen("Hello\n")
printSkyBlue("Hello\n")
printRed("Hello\n")
printPink("Hello\n")
printYellow("Hello\n")
printWhite("Hello\n")
printWhiteBlack("Hello\n")
printWhiteBlack_2("Hello\n")
printYellowRed("Hello\n")