简体   繁体   English

Python 3打印样式

[英]Python 3 print style

I found the following code on Stackoverflow for formatting text in Python. 我在Stackoverflow上找到了以下代码,用于在Python中格式化文本。 (I'm running Python 3 on a Raspberry Pi.) (我正在Raspberry Pi上运行Python3。)

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 确保您使用的是能够显示颜色的终端,并且环境变量TERM设置正确

On starting python as TERM=xterm python on a gnome-terminal shows colored text with your code 在gnome终端上以TERM=xterm python启动python时,会在代码中显示彩色文本

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. 如果您尝试在IDLE下运行此代码,则ANSI转义代码在那里将无法正常工作-IDLE正在模拟最小终端,并为其自己的警告和错误消息使用了一些颜色。

The code works fine here under the default lxterminal on Raspbian as well as the native (non-GUI) linux console. 该代码在Raspbian上的默认lxterminal以及本机(非GUI)Linux控制台下可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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