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

您所在的位置:网站首页 php输出字体颜色不一致 Python编程:pycharm控制台字体颜色

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

2024-07-05 21:38| 来源: 网络整理| 查看: 265

使用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)))

这里写图片描述这里写图片描述

当然,能不能在sublime中显示呢? 这里写图片描述

好吧,那我去windows控制台试试 这里写图片描述

既然这样,只能在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") # 红色背景加下划线

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

通过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)

这里写图片描述

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

通过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")) # 红色

这里写图片描述

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

学过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))

这里写图片描述

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

如果有更好的改进方法,请及时邮件我([email protected]),帮我改进我的代码,以便方便更多的人。

目前已经可以通过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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3