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

相关文章
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
661 3
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
880 3
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
725 3
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
1031 0
|
人工智能 IDE 开发工具
JetBrains PyCharm 2025.1 发布 - 面向专业开发者的 Python IDE
JetBrains PyCharm 2025.1 (macOS, Linux, Windows) - 面向专业开发者的 Python IDE
1815 29
JetBrains PyCharm 2025.1 发布 - 面向专业开发者的 Python IDE
|
IDE 开发工具 开发者
手把手教你安装PyCharm 2025:开发者的Python IDE配置全流程+避坑指南
本教程详细介绍了PyCharm 2025版本在Windows系统下的安装流程及配置方法,涵盖AI代码补全与智能调试工具链等新功能。内容包括系统要求、安装步骤、首次运行配置(如主题选择与插件安装)、创建首个Python项目,以及常见问题解决方法。此外,还提供了切换中文界面和延伸学习资源的指导,帮助用户快速上手并高效使用PyCharm进行开发。
6844 61
|
IDE 测试技术 项目管理
【新手必看】PyCharm2025 免费下载安装配置教程+Python环境搭建、图文并茂全副武装学起来才嗖嗖的快,绝对最详细!
PyCharm是由JetBrains开发的Python集成开发环境(IDE),专为Python开发者设计,支持Web开发、调试、语法高亮、项目管理、代码跳转、智能提示、自动完成、单元测试和版本控制等功能。它有专业版、教育版和社区版三个版本,其中社区版免费且适合个人和小型团队使用,包含基本的Python开发功能。安装PyCharm前需先安装Python解释器,并配置环境变量。通过简单的步骤即可在PyCharm中创建并运行Python项目,如输出“Hello World”。
【新手必看】PyCharm2025 免费下载安装配置教程+Python环境搭建、图文并茂全副武装学起来才嗖嗖的快,绝对最详细!
|
IDE 网络安全 开发工具
IDE之pycharm:专业版本连接远程服务器代码,并配置远程python环境解释器(亲测OK)。
本文介绍了如何在PyCharm专业版中连接远程服务器并配置远程Python环境解释器,以便在服务器上运行代码。
7542 0
IDE之pycharm:专业版本连接远程服务器代码,并配置远程python环境解释器(亲测OK)。
|
数据库 Python Windows
Python:Pycharm安装指南
1. 双击安装程序,点击Next。 2. 选择安装路径,Next。 3. 默认选项,添加环境变量,Next。 4. 保持默认设置,点击Install。 5. 安装完成后选择是否立即重启。
671 2
Python:Pycharm安装指南
|
JSON 数据格式 Python
Flask实现内部接口----pycharm安装及新建,location代表着文件路径,下面是Python的环境,Flask是由Python开发的框架,Python文件接口ython通过GET发送
Flask实现内部接口----pycharm安装及新建,location代表着文件路径,下面是Python的环境,Flask是由Python开发的框架,Python文件接口ython通过GET发送

推荐镜像

更多