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

相关文章
|
Java UED Python
【10月更文挑战第4天】「Mac上学Python 4」入门篇4 - PyCharm高效开发环境配置与使用技巧
本篇将详细介绍如何高效地使用PyCharm进行Python开发,内容涵盖PyCharm的主题设置、字体调整、常用快捷键、虚拟环境的管理、库安装与调试技巧等。通过本篇的学习,用户将能够充分利用PyCharm的功能,提升Python开发效率。
754 2
【10月更文挑战第4天】「Mac上学Python 4」入门篇4 - PyCharm高效开发环境配置与使用技巧
|
前端开发 计算机视觉 Python
浅蓝色代表什么颜色?——Python中的颜色表示与处理
本文介绍了浅蓝色在计算机图形和Web开发中的表示方法,包括RGB、十六进制和HSL三种常见格式,并详细说明了如何使用Python的Pillow和colorsys库来处理和转换这种颜色,最后给出了生成浅蓝色背景的CSS代码示例。
1954 6
|
Python
灵码回复消息的字体太小,并在pycharm中没法设置.Baidu Comate就可以直接插件中设置了
在使用灵码回复消息时,字体过小且在PyCharm中无法调整。而Baidu Comate插件则可以在插件设置中直接修改字体大小,提供更好的阅读体验。
1167 78
|
IDE 测试技术 项目管理
【新手必看】PyCharm2025 免费下载安装配置教程+Python环境搭建、图文并茂全副武装学起来才嗖嗖的快,绝对最详细!
PyCharm是由JetBrains开发的Python集成开发环境(IDE),专为Python开发者设计,支持Web开发、调试、语法高亮、项目管理、代码跳转、智能提示、自动完成、单元测试和版本控制等功能。它有专业版、教育版和社区版三个版本,其中社区版免费且适合个人和小型团队使用,包含基本的Python开发功能。安装PyCharm前需先安装Python解释器,并配置环境变量。通过简单的步骤即可在PyCharm中创建并运行Python项目,如输出“Hello World”。
【新手必看】PyCharm2025 免费下载安装配置教程+Python环境搭建、图文并茂全副武装学起来才嗖嗖的快,绝对最详细!
|
Python
在python终端中打印颜色的3中方式(python3经典编程案例)
这篇文章介绍了在Python终端中打印彩色文本的三种方式:使用`colorama`模块、`termcolor`模块和ANSI转义码。
1116 8
python在控制台打印爱心3-4
python在控制台打印爱心3-4
|
Python Windows
安装python 以及 安装 pycharm
安装python 以及 安装 pycharm
|
开发工具 Python Windows
【Python专栏】搭建Pyhthon运行环境及开发环境 | 安装Python | 安装PyCharm
【Python专栏】搭建Pyhthon运行环境及开发环境 | 安装Python | 安装PyCharm
857 4
|
Python
Python 获取控制台输入的值
Python 获取控制台输入的值
207 0
|
Linux UED iOS开发
Python colorama 设置控制台、命令行输出彩色文字
Python colorama 设置控制台、命令行输出彩色文字
482 0

推荐镜像

更多