Print in terminal with colors?

2018-12-31 01:24发布

How can I output colored text to the terminal, in Python? What is the best Unicode symbol to represent a solid block?

30条回答
若你有天会懂
2楼-- · 2018-12-31 01:53

If you are programming a game perhaps you would like to change the background color and use only spaces? For example:

print " "+ "\033[01;41m" + " " +"\033[01;46m"  + "  " + "\033[01;42m"
查看更多
深知你不懂我心
3楼-- · 2018-12-31 01:54

The answer is Colorama for all cross-platform coloring in Python.

A Python 3.6 example screenshot: example screenshot

查看更多
裙下三千臣
4楼-- · 2018-12-31 01:55

I have wrapped @joeld answer into a module with global functions that I can use anywhere in my code.

file: log.py

HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = "\033[1m"

def disable():
    HEADER = ''
    OKBLUE = ''
    OKGREEN = ''
    WARNING = ''
    FAIL = ''
    ENDC = ''

def infog( msg):
    print OKGREEN + msg + ENDC

def info( msg):
    print OKBLUE + msg + ENDC

def warn( msg):
    print WARNING + msg + ENDC

def err( msg):
    print FAIL + msg + ENDC

use as follows:

 import log
    log.info("Hello World")
    log.err("System Error")
查看更多
弹指情弦暗扣
5楼-- · 2018-12-31 01:55

You can use the Python implementation of the curses library: http://docs.python.org/library/curses.html

Also, run this and you'll find your box:

for i in range(255):
    print i, chr(i)
查看更多
皆成旧梦
6楼-- · 2018-12-31 01:55

note how well the with keyword mixes with modifiers like these that need to be reset (using Python 3 and Colorama):

from colorama import Fore, Style
import sys

class Highlight:
  def __init__(self, clazz, color):
    self.color = color
    self.clazz = clazz
  def __enter__(self):
    print(self.color, end="")
  def __exit__(self, type, value, traceback):
    if self.clazz == Fore:
      print(Fore.RESET, end="")
    else:
      assert self.clazz == Style
      print(Style.RESET_ALL, end="")
    sys.stdout.flush()

with Highlight(Fore, Fore.GREEN):
  print("this is highlighted")
print("this is not")
查看更多
浅入江南
7楼-- · 2018-12-31 01:56

sty is similar to colorama, but it's less verbose, supports 8bit and 24bit (rgb) colors, allows you to register your own colors, is really flexible and well documented. If you don't care about compatibility with terminal emulators that are stuck in the 90th and like to use new features, you may want to give it a try.

from sty import fg, bg, ef, rs, Rule

foo = fg.red + 'This is red text!' + fg.rs
bar = bg.blue + 'This has a blue background!' + bg.rs
baz = ef.italic + 'This is italic text' + rs.italic
qux = fg(201) + 'This is pink text using 8bit colors' + fg.rs
qui = fg(255, 10, 10) + 'This is red text using 24bit colors.' + fg.rs

# Add new colors:

fg.orange = Rule('rgb_fg', 255, 150, 50)

buf = fg.orange + 'Yay, Im orange.' + fg.rs


print(foo, bar, baz, qux, qui, buf, sep='\n')

prints:

enter image description here

Demo: enter image description here

查看更多
登录 后发表回答