print(Style.BRIGHT + Back.YELLOW + Fore.RED + "Colorama ")") 简单的变色函数 background_color_dict={ 'BLACK':40, 'RED':41, 'GREEN':42, 'YELLOW':43, 'BLUE':44, 'MAGENTA':45, 'CYAN':46, 'WHITE':47 } text_color_dict={ '
def print_colored_text(text, color_code): print(f"\033[{color_code}m{text}\033[0m") # 示例:打印红色文本 print_colored_text("这是红色文本", "31") 在上面的代码中,\033[是ANSI转义序列的开始,31代表红色,m是命令的结束标志,\033[0m用于重置颜色和样式。 2. 使用colorama库 colorama是一个...
「安装:」pip install termcolorcprint() 功能相当于print(colored(something))。cprint('sometext','color','on_color',attrs=[])其中:color:文字颜色。可选,grey,red,green,yellow,blue,magenta,cyan,white。on_color:背景色,可选,on_grey,on_red,on_green,on_yellow,on_blue,on_magenta...
接下来,我们需要定义一个函数print_color来实现给输出添加颜色的功能。该函数接受两个参数:输出内容和颜色。 defprint_color(text,color):sys.stdout.write(color+text+COLOR_RESET+'\n') 1. 2. 这个函数的作用是将指定颜色的输出内容打印到控制台上。sys.stdout.write用于打印输出,COLOR_RESET用于重置颜色。 步...
(color) def reset_color(): set_cmd_text_color(FOREGROUND_DARKWHITE) def cprint(text, color): set_text_color(color) print(text) reset_color() if __name__ == '__main__': for k in D_COLOR: cprint(f'我是{k}色文字', k) set_text_color('天蓝') print('-' * 30) input('...
在Python中,可以使用ANSI转义序列来设置print输出的颜色。以下是一个简单的示例: defprint_colored(text, color_code):""" 参数1:要打印的文本 参数2:颜色代码(例如:\033[31m 为红色,\033[32m 为绿色) """print(f"{color_code}{text}\033[0m")# 示例用法red_color ="\033[31m"green_color ="\033...
defprint_color(text,color):print(color+text+colorama.Style.RESET_ALL) 1. 2. 在这个函数中,我们首先将给定的颜色添加到要打印的文字前面。然后,使用colorama.Style.RESET_ALL将颜色重置为默认值,以免影响后续的打印。 使用函数打印不同颜色的文字
RESET = '\033[0m' # Resets the color to default print(f"{RED}This is red text{RESET}") print(f"{GREEN}This is green text{RESET}") print(f"{YELLOW}This is yellow text{RESET}") print(f"{BLUE}This is blue text{RESET}") print(f"{MAGENTA}This is magenta text{RESET}") print(...
color_print('I LOVE YOU', fore='y', back='k') color_print('I HATE YOU', fore='r', back='g', mode='u') 输出:函数体: def color_print(text: str, mode: str = '', fore: str = '', back: str = '') -> None: dict_mode = {'d': '0', 'h': '1', 'nb': '22...
kernel32.SetConsoleTextAttribute(handle,color)print("彩色文本")fg,bg=7,0# 恢复背景黑、前景白color...