简体   繁体   English

在 python shell 上为文本着色

[英]Color a Text on the python shell

I've made several attempts.我已经做了几次尝试。

The first code I've tried is this one:我试过的第一个代码是这个:

import colorama
from colorama import Fore

print(Fore.RED + 'This text is red in color')

but the output is not what I was expecting但输出不是我所期望的

在此处输入图像描述

then I've tried this:然后我试过这个:

import sys
from termcolor import colored, cprint
text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)

but again the result is not correct但结果又不正确

在此处输入图像描述

is there a kind of a setting to setup in order to see the colored text or is it just a problem of python version?是否有一种设置可以设置以查看彩色文本,还是只是 python 版本的问题? The version I'm using as shell is 3.10.4我用作外壳的版本是 3.10.4

The code is working perfectly:代码运行良好:

Try restarting the text editor or something.尝试重新启动文本编辑器或其他东西。

I don't know colorama and termcolor but I can suggest what I did in the past for colored text我不知道 colorama 和 termcolor 但我可以建议我过去为彩色文本所做的事情

def display_blue(msg):
    print('\033[94m'+msg+'\033[0m')

def display_red(msg):
    print('\033[91m'+msg+'\033[0m')

def display_green(msg):
    print('\033[92m'+msg+'\033[0m')

def display_yellow(msg):
    print('\033[93m'+msg+'\033[0m')

I hope it would be helpful我希望它会有所帮助

import colorama
from colorama import Fore
print(f"{Fore.RED}This is Red Color")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM