【问题标题】:instead of getting a color i get this [31mHello World !!![0m using termcolor and colored我没有得到颜色,而是得到了这个 [31mHello World !!![0m using termcolor and coloured
【发布时间】:2019-06-16 16:16:02
【问题描述】:

我尝试使用不同的方法,结果在 cmd 和 poweshell 中是相同的,但是我无法让它显示颜色。我使用的代码:

from termcolor import *
print(colored('Hello World !!!', "red"))

输出:

"[31mHello World !!![0m"

我在“Hello World !!!”周围得到了这个奇怪的字符而且它看起来没有颜色。

【问题讨论】:

  • 那些“奇怪的字符[原文如此]” 颜色代码:en.wikipedia.org/wiki/ANSI_escape_code#Colors
  • 并非所有控制台都支持用于着色的 ANSI 转义码(除其他外)。您当前的控制台似乎不支持它们
  • 但是powershell呢?它不支持着色吗?什么控制台支持着色?有什么办法可以在我的程序中实现它?

标签: python python-3.x python-2.7 colors termcolor


【解决方案1】:

如果您在 linux 中运行您的代码,它会为您提供所需的输出,而不是在 Windows 中。 Powershell 确实支持输出着色,您需要指定 -ForegroundColor 参数,如

Write-Host "PREREQUISITIES" -ForegroundColor Cyan

【讨论】:

  • Powershell 和 CMD 控制台
猜你喜欢
  • 2023-01-29
  • 2021-03-08
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多