Print several sentences with different colors

一笑奈何 提交于 2019-12-13 08:00:58

问题


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)

回答1:


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 :)



来源:https://stackoverflow.com/questions/44661336/print-several-sentences-with-different-colors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!