python colorama打印所有颜色

python colorama打印所有颜色,python,properties,colorama,Python,Properties,Colorama,我是新来学习python的,我是从美国来的。作为一个测试项目,我想用colorama打印出所有可用的颜色 from colorama import Fore from colorama import init as colorama_init colorama_init(autoreset=True) colors = [x for x in dir(Fore) if x[0] != "_"] for color in colors: print(color + f"{color}

我是新来学习python的,我是从美国来的。作为一个测试项目,我想用colorama打印出所有可用的颜色

from colorama import Fore
from colorama import init as colorama_init

colorama_init(autoreset=True)

colors = [x for x in dir(Fore) if x[0] != "_"]
for color  in colors:
    print(color + f"{color}")
ofc输出所有黑色输出,如下所示:

BLACKBLACK
BLUEBLUE
CYANCYAN
...
因为Dir(Fore)只给了我Fore.BLUE,Fore.GREEN

它们是否是访问所有Fore Color属性的一种方式,以便它们实际像中一样工作

print(Fore.BLUE + "Blue")
或者换句话说,这可以更好地表达我的问题:

我想写这个:

print(Fore.BLACK + 'BLACK')
print(Fore.BLUE + 'BLUE')
print(Fore.CYAN + 'CYAN')
print(Fore.GREEN + 'GREEN')
print(Fore.LIGHTBLACK_EX + 'LIGHTBLACK_EX')
print(Fore.LIGHTBLUE_EX + 'LIGHTBLUE_EX')
print(Fore.LIGHTCYAN_EX + 'LIGHTCYAN_EX')
print(Fore.LIGHTGREEN_EX + 'LIGHTGREEN_EX')
print(Fore.LIGHTMAGENTA_EX + 'LIGHTMAGENTA_EX')
print(Fore.LIGHTRED_EX + 'LIGHTRED_EX')
print(Fore.LIGHTWHITE_EX + 'LIGHTWHITE_EX')
print(Fore.LIGHTYELLOW_EX + 'LIGHTYELLOW_EX')
print(Fore.MAGENTA + 'MAGENTA')
print(Fore.RED + 'RED')
print(Fore.RESET + 'RESET')
print(Fore.WHITE + 'WHITE')
print(Fore.YELLOW + 'YELLOW')
简而言之:

for color in all_the_colors_that_are_available_in_Fore:
    print('the word color in the representing color')
    #or something like this?
    print(Fore.color + color)

帕特里克在对这个问题的评论中很好地描述了为什么要打印两次颜色名称

是访问所有Fore Color属性的一种方式,因此它们实际上可以像中一样工作

根据:

您可以使用其他方式打印彩色字符串,例如
print(Fore.RED+'some RED text')

您可以使用
termcolor
模块中的函数,该函数接受一个字符串和一种颜色来为该字符串着色。但并非所有的
Fore
颜色都受支持,因此您可以执行以下操作:

from colorama import Fore
from colorama import init as colorama_init
from termcolor import colored

colorama_init(autoreset=True)

colors = [x for x in dir(Fore) if x[0] != "_"]
colors = [i for i in colors if i not in ["BLACK", "RESET"] and "LIGHT" not in i] 

for color  in colors:
    print(colored(color, color.lower()))
希望这能回答你的问题

编辑:

我阅读了有关
Fore
项的更多信息,发现您可以检索一个字典,其中包含每种颜色作为键,其代码作为值,因此您可以执行以下操作以包含
Fore
中的所有颜色:

from colorama import Fore
from colorama import init as colorama_init

colorama_init(autoreset=True)

colors = dict(Fore.__dict__.items())

for color in colors.keys():
    print(colors[color] + f"{color}")


我重新措辞了我的问题:我想实现的目标是否更清晰?(在下面读:或者换言之,这可以更好地表达我的问题:)谢谢你的回答,效果很好。但不是所有的颜色。例如浅色。我刚刚编辑了我的答案来解决这个问题,请检查:)嗨,你能告诉我如何在其中添加自定义颜色吗?就像我想用橙色和灰色,但不是there@AbdulHaseeb在这种情况下,您必须使用颜色代码,请选中此项。