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

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

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

pycharm版本:Community Edition 2017.2.3

使用格式为:

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

颜色取值:

- 30-37 : 字体颜色

- 40-47 -:背景颜色

显示方式:

- 1 :正常

- 4 :下划线

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


效果测试

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

p43.1.png


当然,能不能在sublime中显示呢?

p43.2.png


好吧,那我去windows控制台试试

p43.3.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")  # 红色背景加下划线

p43.4.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)

p43.5.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"))  # 红色

p43.6.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))

p43.7.png


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


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


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

pip install consolecolor
# 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

相关文章
|
4月前
|
机器学习/深度学习 存储 设计模式
Python 高级编程与实战:深入理解性能优化与调试技巧
本文深入探讨了Python的性能优化与调试技巧,涵盖profiling、caching、Cython等优化工具,以及pdb、logging、assert等调试方法。通过实战项目,如优化斐波那契数列计算和调试Web应用,帮助读者掌握这些技术,提升编程效率。附有进一步学习资源,助力读者深入学习。
|
1月前
|
Python
Python编程基石:整型、浮点、字符串与布尔值完全解读
本文介绍了Python中的四种基本数据类型:整型(int)、浮点型(float)、字符串(str)和布尔型(bool)。整型表示无大小限制的整数,支持各类运算;浮点型遵循IEEE 754标准,需注意精度问题;字符串是不可变序列,支持多种操作与方法;布尔型仅有True和False两个值,可与其他类型转换。掌握这些类型及其转换规则是Python编程的基础。
161 33
|
13天前
|
数据采集 分布式计算 大数据
不会Python,还敢说搞大数据?一文带你入门大数据编程的“硬核”真相
不会Python,还敢说搞大数据?一文带你入门大数据编程的“硬核”真相
34 1
|
1月前
|
设计模式 安全 Python
Python编程精进:正则表达式
正则表达式是一种强大的文本处理工具,用于搜索、匹配和提取模式。本文介绍了正则表达式的语法基础,如`\d`、`\w`等符号,并通过实例展示其在匹配电子邮件、验证电话号码、处理日期格式等场景中的应用。同时,文章提醒用户注意性能、编码、安全性等问题,避免常见错误,如特殊字符转义不当、量词使用错误等。掌握正则表达式能显著提升文本处理效率,但需结合实际需求谨慎设计模式。
|
1月前
|
人工智能 自然语言处理 测试技术
🧠 用 AI 提升你的编程效率 —— 在 PyCharm 中体验通义灵码
通义灵码是一款基于大模型的智能编程辅助工具,现已上线PyCharm插件V2.5+版本。它能根据自然语言描述、注释或上下文生成高质量代码,支持多语言(Python、Java等),提供代码补全、优化建议、单元测试生成及异常排查等功能。集成魔搭MCP市场3000+服务,具备编程智能体模式与长期记忆能力,助开发者提升效率。适用初学者、资深开发者及团队协作场景。小红书、B站、抖音、微博均有相关资源分享。 小红书: http://xhslink.com/a/SvabuxSObf3db bilibili:https://b23.tv/1HJAdIx 抖音: https://v.douyin.com/1DAG
469 3
|
2月前
|
数据采集 安全 BI
用Python编程基础提升工作效率
一、文件处理整明白了,少加两小时班 (敲暖气管子)领导让整理100个Excel表?手都干抽筋儿了?Python就跟铲雪车似的,哗哗给你整利索!
92 11
|
4月前
|
人工智能 Java 数据安全/隐私保护
[oeasy]python081_ai编程最佳实践_ai辅助编程_提出要求_解决问题
本文介绍了如何利用AI辅助编程解决实际问题,以猫屎咖啡的购买为例,逐步实现将购买斤数换算成人民币金额的功能。文章强调了与AI协作时的三个要点:1) 去除无关信息,聚焦目标;2) 将复杂任务拆解为小步骤,逐步完成;3) 巩固已有成果后再推进。最终代码实现了输入验证、单位转换和价格计算,并保留两位小数。总结指出,在AI时代,人类负责明确目标、拆分任务和确认结果,AI则负责生成代码、解释含义和提供优化建议,编程不会被取代,而是会更广泛地融入各领域。
144 28
|
4月前
|
人工智能 自然语言处理 测试技术
在PyCharm中提升编程效率:通义灵码(DeepSeek)助手全攻略(新版)
最近小栈在PyCharm中使用了阿里的 通义灵码 插件还不错,本次就再分享一个好用的AI代码助手,让编码过程更加方便!
662 16
|
4月前
|
机器学习/深度学习 数据可视化 TensorFlow
Python 高级编程与实战:深入理解数据科学与机器学习
本文深入探讨了Python在数据科学与机器学习中的应用,介绍了pandas、numpy、matplotlib等数据科学工具,以及scikit-learn、tensorflow、keras等机器学习库。通过实战项目,如数据可视化和鸢尾花数据集分类,帮助读者掌握这些技术。最后提供了进一步学习资源,助力提升Python编程技能。
|
4月前
|
Python
[oeasy]python074_ai辅助编程_水果程序_fruits_apple_banana_加法_python之禅
本文回顾了从模块导入变量和函数的方法,并通过一个求和程序实例,讲解了Python中输入处理、类型转换及异常处理的应用。重点分析了“明了胜于晦涩”(Explicit is better than implicit)的Python之禅理念,强调代码应清晰明确。最后总结了加法运算程序的实现过程,并预告后续内容将深入探讨变量类型的隐式与显式问题。附有相关资源链接供进一步学习。
66 4

热门文章

最新文章

推荐镜像

更多