简体   繁体   中英

Python 3 print style

I found the following code on Stackoverflow for formatting text in Python. (I'm running Python 3 on a Raspberry Pi.)

class style:
   PURPLE = '\033[95m'
   CYAN = '\033[96m'
   DARKCYAN = '\033[36m'
   BLUE = '\033[94m'
   GREEN = '\033[92m'
   YELLOW = '\033[93m'
   RED = '\033[91m'
   BOLD = '\033[1m'
   UNDERLINE = '\033[4m'
   END = '\033[0m'

print (style.RED + 'SenseHat Temperature' + style.END)

But, I get the following results:

[91mSenseHat Temperature[0m

Make sure you are using a terminal capable of showing colors and the env variable TERM is set properly

On starting python as TERM=xterm python on a gnome-terminal shows colored text with your code

If you are trying to run this under IDLE, ANSI escape codes won't work properly there - IDLE is simulating a minimal terminal and uses some colors for it's own warning and error messages.

The code works fine here under the default lxterminal on Raspbian as well as the native (non-GUI) linux console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM