Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

colorama - Print several sentences with different colors

I'm trying to print several sentences with different colors, but it won't work, I only got 2 colors, the normal blue and this red

import sys
from colorama import init, AnsiToWin32

stream = AnsiToWin32(sys.stderr).stream

print(">>> This is red.", file=stream)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As discussed in the comments, change your code to use these features;

import os, colorama
from colorama import Fore,Style,Back
colorama.init()

print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.BRIGHT + 'and in bright text')
print(Style.RESET_ALL)
print('back to normal now')

These are much easier to use and do job. The available colours you can use for Fore or Back are;

  • Red
  • Cyan
  • Green
  • Yellow
  • Black
  • Magenta
  • Blue
  • White

These will all be needed to be put in capitals. And for Style you can use;

  • Bright
  • Dim
  • Reset_all

These will also need to be in capitals.

Have fun using colorama :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...