How can I output colored text to the terminal, in Python? What is the best Unicode symbol to represent a solid block?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
My favorite way is with the Blessings library (full disclosure: I wrote it). For example:
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:
You can print in specific locations as well:
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:
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!
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:
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:Outputs "Hello, world!" with the words in blue and the exclamation mark bold red and blinking.
You could use CLINT:
Get it from GitHub.
YAY! another version
while i find this answer useful, i modified it a bit. this Github Gist is the result
usage
in addition you can wrap common usages:
https://gist.github.com/Jossef/0ee20314577925b4027f