本帖最后由 Cool_Breeze 于 2020-12-19 08:38 编辑
[Python] 纯文本查看 复制代码 # coding=utf-8
# SetConsoleTextAttribute 函数是靠一个字节的低四位来控制前景色,高四位来控制背景色。(0b00000000 - 0b11111111)
# H 开头高亮
import ctypes, sys
class CmdColor(object):
_STD_INPUT_HANDLE = -10
_STD_OUTPUT_HANDLE = -11
_STD_ERROR_HANDLE = -12
F_BLACK = 0x00
F_BLUE = 0x01
F_GREEN = 0x02
F_SKYBLUE = 0x03
F_RED = 0x04
F_VIOLET = 0x05
F_YELLOW = 0x06
F_WHITE = 0x07
F_GRAY = 0x08
F_HBLUE = 0x09
F_HGREEN = 0x0A
F_HSKYBLUE = 0x0B
F_HRED = 0x0C
F_HVIOLET = 0x0D
F_HYELLOW = 0x0E
F_HWHITE = 0x0F
B_BLACK = 0x00
B_BLUE = 0x10
B_GREEN = 0x20
B_SKYBLUE = 0x30
B_RED = 0x40
B_VIOLET = 0x50
B_YELLOW = 0x60
B_WHITE = 0x70
B_GRAY = 0x80
B_HBLUE = 0x90
B_HGREEN = 0xA0
B_HSKYBLUE = 0xB0
B_HRED = 0xC0
B_HVIOLET = 0xD0
B_HYELLOW = 0xE0
B_HWHITE = 0xF0
def __init__(self):
self._handle = ctypes.windll.kernel32.GetStdHandle(
CmdColor._STD_OUTPUT_HANDLE)
def setColor(self, color):
ctypes.windll.kernel32.SetConsoleTextAttribute(self._handle, color)
def reSetColor(self):
self.setColor(self.F_WHITE)
def print(self,color, mess):
self.setColor(color)
sys.stdout.write(mess)
sys.stdout.flush()
self.reSetColor()
if __name__ == '__main__':
color = CmdColor()
# color.setColor(color.F_HYELLOW | color.B_BLACK)
# print('Cool_Breeze')
# print('Cool_Breeze')
# color.reSetColor()
# 需要手动添加换行符
# color.print((color.F_HSKYBLUE | color.B_GRAY), 'Cool_Breeze\n')
# color.print(0x8B, 'Cool_Breeze\n')
for n in range(1,256,1):
color.print(n, f'{n:<4} hex:{bin(n):<12}')
if n%5 == 0 :print()
结果图:
|