自己研究的,不知道以前有没有人发过。比较实用,使用时把echo函数复制粘贴一下就行了
[Python] 纯文本查看 复制代码#!/usr/bin/python
# -*- coding: UTF-8 -*-

import ctypes
def echo(*o,c=7,sep=\’ \’,end=\’\\n\’):k=ctypes.WinDLL("Kernel32.dll");h=k.GetStdHandle(-11);f=k.SetConsoleTextAttribute;f(h,c);print(*o,sep=sep,end=end,flush=True);f(h,7)

\’\’\’下面三行不是必须的,加上它会使程序更加严谨\’\’\’
import signal
def keyboard_handler(signum, frame):k=ctypes.WinDLL("Kernel32.dll");h=k.GetStdHandle(-11);f=k.SetConsoleTextAttribute;f(h,7);exit(0)
signal.signal(signal.SIGINT, keyboard_handler)
\’\’\’
-参数c 高8位:背景色 低8位:前景色 (8421-高红绿蓝)
\’\’\’
def test():
echo("[WARN]",c=10,end=" ")
echo("echo函数只在windows命令行下有效")
echo("[INFO]",c=9,end=" ")
echo("echo函数在PowerShell下会改变蓝色背景")
echo("[ERROR]",c=13,end="不支持flush=False;file参数只支持stdout,若要写入文件,可使用原生print函数\\n")
echo("[FATAL]",c=12,end="可以调用GetConsoleScreenBufferInfo完善echo函数\\n\\n")
echo(["打","印","列","表"],c=14)
echo("顺","序","打","印",sep=",",c=9)
echo("黄底绿字",c=0xE2)

if __name__ == \’__main__\’:
test()

image.png