如何在IDLE的终端中打印彩色文本? [英] How do I print colored text in IDLE's terminal?

查看:160
本文介绍了如何在IDLE的终端中打印彩色文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很容易是一个重复的问题,因为它是。但是,对此有很多不足的答案(例如,尝试 curses !指向26页的文档)。

This is very easily a duplicate question--because it is. However, there are very many inadequate answers to this (e.g. try curses! -- pointing to a 26 page documentation).

在IDLE中输出时,我只想用蓝色以外的颜色打印文本。可能吗?什么是简单的方法?我正在Windows上运行Python 3.6。

I just want to print text in a color other than blue when I'm outputting in IDLE. Is it possible? What's an easy way to do this? I'm running Python 3.6 on Windows.

请举例说明。

(我发现ANSI代码仅在终端上无法在IDLE内部使用。)

(I have found that ANSI codes do not work inside IDLE, only on the terminal.)

推荐答案

将此代码放在代码的开始位置:

Put this at the "start" of your code:

import sys

try: color = sys.stdout.shell
except AttributeError: raise RuntimeError("Use IDLE")

然后使用 color.write( YourText,Color)进行打印:

color.write("Hi, are you called Miharu461? \n","KEYWORD")
color.write("Yes","STRING")
color.write(" or ","KEYWORD")
color.write("No\n","COMMENT")

此打印:

注意:这不会自动放入回车符(例如 print 函数/语句)。因此,在打印时,将 \n 放在最后一个字符串的末尾。

Note: this does NOT automatically puts the enter (like in the print function/statement). So, when you are printing put \n at the end of the last string to put it.

您可以输入的颜色是: SYNC stdin BUILTIN STRING 控制台 COMMENT stdout TODO stderr hit 定义关键字错误 sel

The "Colors" you can put are: SYNC, stdin, BUILTIN, STRING, console, COMMENT, stdout, TODO, stderr, hit, DEFINITION, KEYWORD, ERROR, and sel.

注意2 :这取决于颜色您用于IDLE的方案。因此,我建议您将其用于突出显示,而不要用于编写程序来询问某个颜色是什么颜色。

Note 2: This is dependent of the color scheme you are using for IDLE. So I recommend you to use it for highlighting, and not for making a program for asking what in color is some word.

这篇关于如何在IDLE的终端中打印彩色文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆