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
If you are programming a game perhaps you would like to change the background color and use only spaces? For example:
The answer is Colorama for all cross-platform coloring in Python.
A Python 3.6 example screenshot:
I have wrapped @joeld answer into a module with global functions that I can use anywhere in my code.
file: log.py
use as follows:
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:
note how well the
with
keyword mixes with modifiers like these that need to be reset (using Python 3 and Colorama):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.
prints:
Demo: