python命令符为啥是黑的啊_Python命令行字体颜色,python

python 命令行常用字体颜色

在平时工作中经常会使用python脚本编写一些程序或工具,经常需要在控制台输出一些信息,例如:请输入用户名、密码等。如果用户名不合法输出‘用户名不规范’等提示信息。但这样输出默认都是黑底白字,不容易区分重要的提示信息,如何才能做的更高端一些呢?

就像 Linux 命令行的提示,像这样:

python命令符为啥是黑的啊_Python命令行字体颜色,python_第1张图片

命令行的提示信息给用户的视觉感受真的很重要,其实我们也可以做到 Linux 命令行这样,废话不多说,上案例。

没有颜色的命令行是这样的:

python命令符为啥是黑的啊_Python命令行字体颜色,python_第2张图片

是不是感觉很low啊!你是不是也是这样写命令行的啊?

有颜色的命令行是这样的:

python命令符为啥是黑的啊_Python命令行字体颜色,python_第3张图片

是不是感觉高端了很多啊,还在等什么,代码在下面,拿走不谢!

class Color(object):

GREEN = '\033[92m'

YELLOW = '\033[93m'

RED = '\033[91m'

BlUE = '\033[94m'

END = '\033[0m'

@classmethod

def red(cls, string):

return cls.RED + string + cls.END

@classmethod

def green(cls, string):

return cls.GREEN + string + cls.END

@classmethod

def yellow(cls, string):

return cls.YELLOW + string + cls.END

@classmethod

def cyan(cls, string):

return cls.BlUE + string + cls.END

if __name__ == '__main__':

green = Color.green('this is green, such as ....ok')+' over'

red = Color.red('this is red, such as error:xxxxxx')+' over'

yellow = Color.yellow('this is yellow, such as warning........')+' over'

blue = Color.cyan('this is blue, such as, information')+' over'

print(green)

print(red)

print(yellow)

print(blue)

您的赞就是我写文章的动力,如果对您有一点点帮助的话,老铁,不要吝啬,请留下您的赞。

你可能感兴趣的:(python命令符为啥是黑的啊)