python实现控制台输出彩色字体

时间:2021-05-22

python控制台输出彩色字体,供大家参考,具体内容如下

import ctypes, sysSTD_INPUT_HANDLE = -10STD_OUTPUT_HANDLE = -11STD_ERROR_HANDLE = -12# 字体颜色定义 text colorsFOREGROUND_BLUE = 0x09 # blue.FOREGROUND_GREEN = 0x0a # green.FOREGROUND_RED = 0x0c # red.FOREGROUND_YELLOW = 0x0e # yellow.# 背景颜色定义 background colorsBACKGROUND_YELLOW = 0xe0 # yellow.# get handlestd_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)def set_cmd_text_color(color, handle=std_out_handle): Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color) return Bool# reset whitedef resetColor(): set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)# greendef printGreen(mess): set_cmd_text_color(FOREGROUND_GREEN) sys.stdout.write(mess + '\n') resetColor()# reddef printRed(mess): set_cmd_text_color(FOREGROUND_RED) sys.stdout.write(mess + '\n') resetColor()# yellowdef printYellow(mess): set_cmd_text_color(FOREGROUND_YELLOW) sys.stdout.write(mess + '\n') resetColor()# white bkground and black textdef printYellowRed(mess): set_cmd_text_color(BACKGROUND_YELLOW | FOREGROUND_RED) sys.stdout.write(mess + '\n') resetColor()

使用

printGreen("你好,世界!")

顺便把图形放出来,供大家使用

replyContent = u'''' _ (\\ _ ___ .-"`"(\\ _.""` `"-. / ` `-._ _.-" `\__ 6 6) `-.__.-' `", / `;-` / , | () / /` | `---`"~``\ | \ | \ \ / / /`, , | | / / "-.| | | /' / / | /,__ | /`\ ljs / /' | / `"'\ ( \ __/ /' | | `\ \ \ \ / | | `\ \ \ `-,/ / | / |-"` `"""^^^ `^^""""` ------------------------------------------------'''printGreen(replyContent)

注意:测试在idea开发工具中打印使用未显示彩色,但在Linux控制台,cmd控制台打均为彩色字体。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章