控制台颜色 (Windows)

发布于 2024-09-14 15:55:13 字数 101 浏览 2 评论 0原文

是否可以在 Windows 的 Python 中打印出不同颜色的东西?我已经启用了 ANSI.sys,但这似乎不起作用。

我希望能够以红色打印一行,以绿色打印下一行,等等。

Is it possible to print out things in different colors in Python for Windows? I already enabled ANSI.sys, but this does not seam to work.

I want to be able to print one line in red, and the next in green, etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2024-09-21 15:55:13

WConio 模块应该是完成此操作所需的全部内容。

WConio.textbackground(color) 设置背景颜色而不更改前景。请参阅下面的颜色常数。

WConio.textcolor(color) 设置前景色而不更改背景。请参阅下面的颜色常数。

它引用的常量实际上并未在页面上列出,而是位于 WConio.py 文件的顶部:

BLACK = 0
BLUE = 1
GREEN = 2
CYAN = 3
RED = 4
MAGENTA = 5
BROWN = 6
LIGHTGRAY = LIGHTGREY = 7
DARKGRAY = DARKGREY = 8
LIGHTBLUE = 9
LIGHTGREEN = 10
LIGHTCYAN = 11
LIGHTRED = 12
LIGHTMAGENTA = 13
YELLOW = 14
WHITE = 15

因此将文本前景色设置为红色的完整调用将是:

WConio.textcolor(WConio.RED)

The WConio module should be all you need to accomplish this.

WConio.textbackground(color) sets the background color without changing the foreground. See below for the color constants.

WConio.textcolor(color) sets the foreground color without changing the background. See below for the color constants.

The constants it refers to are not actually listed on the page, but are at the top of the WConio.py file:

BLACK = 0
BLUE = 1
GREEN = 2
CYAN = 3
RED = 4
MAGENTA = 5
BROWN = 6
LIGHTGRAY = LIGHTGREY = 7
DARKGRAY = DARKGREY = 8
LIGHTBLUE = 9
LIGHTGREEN = 10
LIGHTCYAN = 11
LIGHTRED = 12
LIGHTMAGENTA = 13
YELLOW = 14
WHITE = 15

So a full call to set the text foreground colour to red would be:

WConio.textcolor(WConio.RED)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文