如何在python中打印颜色/颜色? [英] How to print colour/color in python?

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

问题描述

我是 Python 和 StackOverflow 的新手,我需要一点帮助.我想用 Python 打印颜色并在 Google 上搜索,但运气不佳:( 我每次都感到困惑,但都没有奏效.这是我输入的代码.

New to both Python and StackOverflow, I'd like a little help. I'd like to print color in Python and have Googled but with little luck :( I've been confused each time and none has worked. This is the code I have typed.

answer = input ("Wanna go explore? OPTIONS : Yes or No")
if answer == "no":
    print("Awww, come on, don't be like that, lets go!")
elif answer == "yes":
    print ("Great! Lets go!")
else: 
    print("Whats that? I couldn't hear you!")

现在,我希望 OPTIONS 为绿色,Yes 为蓝色,没有红色.如何实现这一目标?

Now, I would like to have OPTIONS colored Green and Yes colored blue and No colored Red. How would one achieve this?

推荐答案

如果您想在 IDLE shell 中打印颜色,使用 ASCI 转义码没有任何帮助,因为它没有实现此功能.

If you want to print color in the IDLE shell no answer using ASCI escape codes will help you as it does not implement this feature.

有一个特定于 IDLE 的 hack 可以让你直接写入它的 PyShell 对象并指定 IDLE 已经定义的文本标签,例如 "STRING" 它将出现默认为绿色.

There is a hack specific to IDLE that lets you write to it's PyShell object directly and specify text tags that IDLE has already defined such as "STRING" which will appear as green by default.

import sys

try:
    shell = sys.stdout.shell
except AttributeError:
    raise RuntimeError("you must run this program in IDLE")

shell.write("Wanna go explore? ","KEYWORD")
shell.write("OPTIONS","STRING")
shell.write(" : ","KEYWORD")
shell.write("Yes","DEFINITION")
shell.write(" or ","KEYWORD")
shell.write("No","COMMENT")
answer = input()

在IDLE下运行会出现这个提示:

When run in IDLE will result in this prompt:

以下是所有可用标签的列表:

Here is a list of all valid tags for use:

print("here are all the valid tags:\n")

valid_tags = ('SYNC', 'stdin', 'BUILTIN', 'STRING', 'console', 'COMMENT', 'stdout',
              'TODO','stderr', 'hit', 'DEFINITION', 'KEYWORD', 'ERROR', 'sel')

for tag in valid_tags:
    shell.write(tag+"\n",tag)

请注意,'sel' 是特殊的,它代表被选中的文本,因此一旦点击其他内容,它将被取消选中.也可以用来开始一些选择复制的文本.

Note that 'sel' is special that it represents the text that is selected, so it will be un-selected once something else is clicked on. As well it can be used to start some text selected for copying.

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

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