Python编程:pycharm控制台字体颜色

简介: Python编程:pycharm控制台字体颜色

使用pycharm写python,发现这个设置字体挺好玩。当然,有时候也很有用,不同版本的pycharm可能格式不一样。


pycharm版本:Community Edition 2017.2.3


使用格式为:


\033[颜色;显示方式m 文字 \033[0m

1

颜色取值:

- 30-37 : 字体颜色

- 40-47 -:背景颜色


显示方式:

- 1 :正常

- 4 :下划线


注意:文字两测不需要空格,其他数字没有效果,有兴趣可以试试


效果测试

 

for i in range(30, 48):
        print("{} \033[{};1mhello\033[0m".format(i, str(i)))

image.png

既然这样,只能在pycharm中玩了


原始方式设置颜色:


 

print("\033[31;1mhello\033[0m")  # 红色
    print("\033[31;4mhello\033[0m")  # 红色加下划线
    print("\033[41;1mhello\033[0m")  # 红色背景
    print("\033[41;4mhello\033[0m")  # 红色背景加下划线

image.png

每次设置感觉看着挺不舒服的,一对乱码,下面我就对其进行一下小小的改造


通过FontColor类变量设置颜色

将已知的效果,统统枚举出来,封装到类中


class FontColor():
    """不同的版本可能颜色不一样
    调用方式:颜色/背景色/+下划线标志 + 需要加颜色的文字 + 结束标志
    """
    # 颜色码
    white = "\033[30;"  # default
    red = "\033[31;"
    green = "\033[32;"
    brown = "\033[33;"
    Pink = "\033[34;"
    Violet = "\033[35;"  # 紫色
    blue = "\033[36;"
    black = "\033[37;"
    # 背景色
    white_background = "\033[40;"  # default
    red_background = "\033[41;"
    green_background = "\033[42;"
    brown_background = "\033[43;"
    Pink_background = "\033[44;"
    Violet_background = "\033[45;"  # 紫色
    blue_background = "\033[46;"
    black_background = "\033[47;"
    # 下划线标志
    default = "0m"  # default
    underline = "4m"
    # 结束标志位
    end = "\033[0m"

通过类变量调用已经写好的枚举值


# 绿色
    print(FontColor.green + FontColor.default + "hello" + FontColor.end)
    # 绿色加下划线
    print(FontColor.green + FontColor.underline + "hello" + FontColor.end)
    # 绿色背景
    print(FontColor.green_background + FontColor.default + "hello" + FontColor.end)
    # 绿色背景加下划线
    print(FontColor.green_background + FontColor.underline + "hello" + FontColor.end)

image.png


额,这个是好看些了,不过代码量变多了,每次都要这么写,好难受


通过FontColor类方法设置颜色

把刚刚写的类,增加两个类方法,分别设置字体颜色和背景颜色


class FontColor():
    """不同的版本可能颜色不一样
    调用方式:颜色/背景色/+下划线标志 + 需要加颜色的文字 + 结束标志
    """
    # 颜色码
    white = "\033[30;"  # default
    red = "\033[31;"
    green = "\033[32;"
    brown = "\033[33;"
    Pink = "\033[34;"
    Violet = "\033[35;"  # 紫色
    blue = "\033[36;"
    black = "\033[37;"
    # 背景色
    white_background = "\033[40;"  # default
    red_background = "\033[41;"
    green_background = "\033[42;"
    brown_background = "\033[43;"
    Pink_background = "\033[44;"
    Violet_background = "\033[45;"  # 紫色
    blue_background = "\033[46;"
    black_background = "\033[47;"
    # 下划线标志
    default = "0m"  # default
    underline = "4m"
    # 结束标志位
    end = "\033[0m"
    # 设置字体色方法
    @classmethod
    def set_color(self, text, color="white", underline=False):
        if hasattr(self, color):
            if underline:
                return getattr(self, color) + self.underline + text + self.end
            else:
                return getattr(self, color) + self.default + text + self.end
        else:
            return text
    # 设置背景色
    @classmethod
    def set_backcolor(self, text, backcolor="white", underline=False):
        color = backcolor + "_background"
        if hasattr(self, color):
            if underline:
                return getattr(self, color) + self.underline + text + self.end
            else:
                return getattr(self, color) + self.default + text + self.end
        else:
            return text

测试一下


print(FontColor.set_color("你好", "red"))  # 红色

image.png


现在是不是就好多了,一目了然。


学过C#的都知道,颜色集合被封装到了枚举类,在IDE(visual studio)中,通过敲首字母就可以看到提示,下面来实现这个功能


将常用颜色封装到Colors这个类中


class Colors():
    # 颜色枚举
    white = "white" # default
    red = "red"
    green = "green"
    brown = "brown"  # 棕色
    Pink = "Pink"
    Violet = "Viole"  #紫色
    blue = "blue"
    black = "black"

再来调用FontColor类中的方法试试


print(FontColor.set_color("你好", Colors.green))  # 绿色
    #  绿色加下划线
    print(FontColor.set_color("你好", Colors.green, underline=True))
    print(FontColor.set_backcolor("你好", Colors.blue))  # 蓝色背景
    #  蓝色背景加下划线
    print(FontColor.set_backcolor("你好", Colors.blue, underline=True))

image.png

好了,下面贴出完整代码,可以将此代码拷贝到python内置库中,或者自己项目里边,以便使用。当然,也可以根据自己的习惯修改代码,使之更加易用。


如果有更好的改进方法,请及时邮件我(pengshiyuyx@gmail.com),帮我改进我的代码,以便方便更多的人。


目前已经可以通过pip安装了


 
         
# consolecolor.py
# 设置控制台字体颜色
# 代码格式:\033[颜色;显示方式m code \033[0m
class Colors():
    # 颜色枚举
    white = "white" # default
    red = "red"
    green = "green"
    brown = "brown"  # 棕色
    Pink = "Pink"
    Violet = "Viole"  #紫色
    blue = "blue"
    black = "black"
class FontColor():
    """不同的版本可能颜色不一样
    调用方式:颜色/背景色/+下划线标志 + 需要加颜色的文字 + 结束标志
    """
    # 颜色码
    white = "\033[30;"  # default
    red = "\033[31;"
    green = "\033[32;"
    brown = "\033[33;"
    Pink = "\033[34;"
    Violet = "\033[35;"  # 紫色
    blue = "\033[36;"
    black = "\033[37;"
    # 背景色
    white_background = "\033[40;"  # default
    red_background = "\033[41;"
    green_background = "\033[42;"
    brown_background = "\033[43;"
    Pink_background = "\033[44;"
    Violet_background = "\033[45;"  # 紫色
    blue_background = "\033[46;"
    black_background = "\033[47;"
    # 下划线标志
    default = "0m"  # default
    underline = "4m"
    # 结束标志位
    end = "\033[0m"
    # 设置字体色方法
    @classmethod
    def set_color(self, text, color="white", underline=False):
        if hasattr(self, color):
            if underline:
                return getattr(self, color) + self.underline + text + self.end
            else:
                return getattr(self, color) + self.default + text + self.end
        else:
            return text
    # 设置背景色
    @classmethod
    def set_backcolor(self, text, backcolor="white", underline=False):
        color = backcolor + "_background"
        if hasattr(self, color):
            if underline:
                return getattr(self, color) + self.underline + text + self.end
            else:
                return getattr(self, color) + self.default + text + self.end
        else:
            return text
if __name__ == "__main__":
    # 颜色测试
    for i in range(30, 48):
        print("{} \033[{};1mhello\033[0m".format(i, str(i)))
    # 1.原始调用方式:
    print("\033[31;1mhello\033[0m")  # 红色
    print("\033[31;4mhello\033[0m")  # 红色加下划线
    print("\033[41;1mhello\033[0m")  # 红色背景
    print("\033[41;4mhello\033[0m")  # 红色背景加下划线
    # 2.通过FontColor类变量设置
    # 绿色
    print(FontColor.green + FontColor.default + "hello" + FontColor.end)
    # 绿色加下划线
    print(FontColor.green + FontColor.underline + "hello" + FontColor.end)
    # 绿色背景
    print(FontColor.green_background + FontColor.default + "hello" + FontColor.end)
    # 绿色背景加下划线
    print(FontColor.green_background + FontColor.underline + "hello" + FontColor.end)
    # 3.通过FontColor类方法设置
    print(FontColor.set_color("你好", "red"))  # 红色
    print(FontColor.set_color("你好", Colors.green))  # 绿色
    #  绿色加下划线
    print(FontColor.set_color("你好", Colors.green, underline=True))
    print(FontColor.set_backcolor("你好", Colors.blue))  # 蓝色背景
    #  蓝色背景加下划线
    print(FontColor.set_backcolor("你好", Colors.blue, underline=True))

代码地址:


https://github.com/mouday/consolecolor


https://github.com/mouday/SomeCodeForPython/tree/master/pycharm_fontcolor


参考文章

《python之设置控制台字体颜色》

http://www.bubuko.com/infodetail-2234862.html

相关文章
|
2天前
|
存储 人工智能 数据挖掘
Python编程入门:从基础到实战
【9月更文挑战第26天】 在这篇文章中,我们将一起探索Python编程的奇妙世界。无论你是初学者还是有一定经验的开发者,这篇文章都将为你提供有价值的信息和技巧。我们将从Python的基本语法开始,然后逐步深入到更复杂的主题,如函数、类和模块。最后,我们将通过一个实际的项目来应用我们所学的知识。让我们一起开始这段Python编程之旅吧!
|
2天前
|
Python
探索Python编程中的装饰器魔法
【9月更文挑战第26天】在Python的世界里,装饰器就像是一把瑞士军刀,小巧而功能强大。它们让代码更简洁、可维护性更强。本文将通过实际示例,带你领略装饰器的魔力,从基础到进阶,一步步揭开它的神秘面纱。
10 2
|
1天前
|
Python
python在控制台打印爱心3-4
python在控制台打印爱心3-4
|
2天前
|
机器学习/深度学习 人工智能 数据挖掘
探索Python编程之美:从基础到进阶
【9月更文挑战第25天】在数字时代的浪潮中,编程已成为一项宝贵的技能。本篇文章将引导你步入Python的奇妙世界,一个既适合初学者又深受资深开发者喜爱的编程语言。我们将一起揭开Python语言的基础面纱,探索它的核心概念,并通过实际示例深入理解其强大功能。无论你是编程新手还是希望提升自己的老手,这篇文章都将为你提供一条清晰的学习路径,助你在编程之旅上更进一步。
|
1天前
|
Python
python编程获取续蜀山剑侠传:从目录名称、网址到内容
python编程获取续蜀山剑侠传:从目录名称、网址到内容
|
1天前
|
移动开发 Python Windows
python编程获取网页标题title的几种方法及效果对比(源代码)
python编程获取网页标题title的几种方法及效果对比(源代码)
|
1天前
|
Python
python编程获取《续蜀山剑侠传》目录信息:目录名称和网址
python编程获取《续蜀山剑侠传》目录信息:目录名称和网址
|
2天前
|
Python
告别低效!Python并查集:数据结构界的超级英雄,拯救你的编程人生!
告别低效!Python并查集:数据结构界的超级英雄,拯救你的编程人生!
8 0
|
2天前
|
Python Windows
安装python 以及 安装 pycharm
安装python 以及 安装 pycharm
|
2天前
|
数据挖掘 Python
Python数据挖掘编程基础
本章介绍了Python数据挖掘编程的基础知识,涵盖Python入门所需的命令、判断与循环、函数、库导入等内容,并重点讲解了数据分析预处理和建模常用库。通过学习基本运算、数据结构、字符串操作等,帮助读者快速掌握Python语言,为后续数据挖掘工作打下坚实基础。例如,通过代码`a=3`进行赋值,利用`a*3`执行乘法运算,使用`a**3`计算幂,以及通过对字符串的拼接和分割操作,展示Python的强大功能。
11 0