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

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

20204582521295.jpg

```python

import ctypes, sys

STD_INPUT_HANDLE = -10

STD_OUTPUT_HANDLE = -11

STD_ERROR_HANDLE = -12

字体颜色定义 text colors

FOREGROUND_BLUE = 0x09 # blue.

FOREGROUND_GREEN = 0x0a # green.

FOREGROUND_RED = 0x0c # red.

FOREGROUND_YELLOW = 0x0e # yellow.

背景颜色定义 background colors

BACKGROUND_YELLOW = 0xe0 # yellow.

get handle

std_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 white

def resetColor():

set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)

green

def printGreen(mess):

set_cmd_text_color(FOREGROUND_GREEN)

sys.stdout.write(mess + '\n')

resetColor()

red

def printRed(mess):

set_cmd_text_color(FOREGROUND_RED)

sys.stdout.write(mess + '\n')

resetColor()

yellow

def printYellow(mess):

set_cmd_text_color(FOREGROUND_YELLOW)

sys.stdout.write(mess + '\n')

resetColor()

white bkground and black text

def printYellowRed(mess):

set_cmd_text_color(BACKGROUND_YELLOW | FOREGROUND_RED)

sys.stdout.write(mess + '\n')

resetColor()```

使用

python

printGreen("你好,世界!")

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

`python

replyContent = u''''

_

(\\ _ ___

.-"`"(\\ _.""` `"-.

/ ` `-._ _.-" `\__

6 6) `-.__.-' `",

/ `;-`

/ , |

() / /` |

`---`"~\ |

\ |

\ \ / /

/, , | | /

/ "-.| | | /'

/ / | /,__ | /\

ljs / /' | / "'\ ( \

__/ /' | |\ \ \

\ / | | \ \ \-,/ / | / |-""""^^^ ^^""""

------------------------------------------------'''

printGreen(replyContent)```

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值