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)





'''

-参数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()

预览:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值