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 02:04

My favorite way is with the Blessings library (full disclosure: I wrote it). For example:

from blessings import Terminal

t = Terminal()
print t.red('This is red.')
print t.bold_bright_red_on_black('Bright red on black')

To print colored bricks, the most reliable way is to print spaces with background colors. I use this technique to draw the progress bar in nose-progressive:

print t.on_green(' ')

You can print in specific locations as well:

with t.location(0, 5):
    print t.on_yellow(' ')

If you have to muck with other terminal capabilities in the course of your game, you can do that as well. You can use Python's standard string formatting to keep it readable:

print '{t.clear_eol}You just cleared a {t.bold}whole{t.normal} line!'.format(t=t)

The nice thing about Blessings is that it does its best to work on all sorts of terminals, not just the (overwhelmingly common) ANSI-color ones. It also keeps unreadable escape sequences out of your code while remaining concise to use. Have fun!

查看更多
高级女魔头
3楼-- · 2018-12-31 02:06

generated a class with all the colors using a for loop to iterate every combination of color up to 100, then wrote a class with python colors. Copy and paste as you will, GPLv2 by me:

class colors:
    '''Colors class:
    reset all colors with colors.reset
    two subclasses fg for foreground and bg for background.
    use as colors.subclass.colorname.
    i.e. colors.fg.red or colors.bg.green
    also, the generic bold, disable, underline, reverse, strikethrough,
    and invisible work with the main class
    i.e. colors.bold
    '''
    reset='\033[0m'
    bold='\033[01m'
    disable='\033[02m'
    underline='\033[04m'
    reverse='\033[07m'
    strikethrough='\033[09m'
    invisible='\033[08m'
    class fg:
        black='\033[30m'
        red='\033[31m'
        green='\033[32m'
        orange='\033[33m'
        blue='\033[34m'
        purple='\033[35m'
        cyan='\033[36m'
        lightgrey='\033[37m'
        darkgrey='\033[90m'
        lightred='\033[91m'
        lightgreen='\033[92m'
        yellow='\033[93m'
        lightblue='\033[94m'
        pink='\033[95m'
        lightcyan='\033[96m'
    class bg:
        black='\033[40m'
        red='\033[41m'
        green='\033[42m'
        orange='\033[43m'
        blue='\033[44m'
        purple='\033[45m'
        cyan='\033[46m'
        lightgrey='\033[47m'
查看更多
忆尘夕之涩
4楼-- · 2018-12-31 02:06

Yet another pypi module that wraps the python 3 print function:

https://pypi.python.org/pypi/colorprint

It's usable in python 2.x if you also from __future__ import print. Here is a python 2 example from the modules pypi page:

from __future__ import print_function
from colorprint import *

print('Hello', 'world', color='blue', end='', sep=', ')
print('!', color='red', format=['bold', 'blink'])

Outputs "Hello, world!" with the words in blue and the exclamation mark bold red and blinking.

查看更多
若你有天会懂
5楼-- · 2018-12-31 02:08

You could use CLINT:

from clint.textui import colored
print colored.red('some warning message')
print colored.green('nicely done!')

Get it from GitHub.

查看更多
妖精总统
6楼-- · 2018-12-31 02:08

YAY! another version

while i find this answer useful, i modified it a bit. this Github Gist is the result

usage

print colors.draw("i'm yellow", bold=True, fg_yellow=True)

enter image description here

in addition you can wrap common usages:

print colors.error('sorry, ')

asd

https://gist.github.com/Jossef/0ee20314577925b4027f

查看更多
萌妹纸的霸气范
7楼-- · 2018-12-31 02:08
# Pure Python 3.x demo, 256 colors
# Works with bash under Linux

fg = lambda text, color: "\33[38;5;" + str(color) + "m" + text + "\33[0m"
bg = lambda text, color: "\33[48;5;" + str(color) + "m" + text + "\33[0m"

def print_six(row, format):
    for col in range(6):
        color = row*6 + col + 4
        if color>=0:
            text = "{:3d}".format(color)
            print (format(text,color), end=" ")
        else:
            print("   ", end=" ")

for row in range(-1,42):
    print_six(row, fg)
    print("",end=" ")
    print_six(row, bg)
    print()

Text with altering foreground and background, colors 0..141 Text with altering foreground and background, colors 142..255

查看更多
登录 后发表回答