Colors 如何在不导入其他模块的情况下更改python中文本的颜色?
如何创建一个打印语句,例如可以是不同颜色(例如绿色)的Colors 如何在不导入其他模块的情况下更改python中文本的颜色?,colors,python-3.7,python-idle,Colors,Python 3.7,Python Idle,如何创建一个打印语句,例如可以是不同颜色(例如绿色)的print(“Hello world”) 另外,有没有一种方法可以在不需要下载新模块的情况下实现这一点?@epicgamer300065,以下所有代码都是从前面提供的@Brian和I(@Scott)链接中提取的。我确实需要在导入之后插入init()语句以使代码正常工作,我确实需要从命令提示符下运行它(但不需要管理员权限),但它确实会产生预期的彩色结果。(仅供参考,我正在win10pro上使用Python 3.8.1): @epicgamer3
print(“Hello world”)
另外,有没有一种方法可以在不需要下载新模块的情况下实现这一点?@epicgamer300065,以下所有代码都是从前面提供的@Brian和I(@Scott)链接中提取的。我确实需要在导入之后插入init()语句以使代码正常工作,我确实需要从命令提示符下运行它(但不需要管理员权限),但它确实会产生预期的彩色结果。(仅供参考,我正在win10pro上使用Python 3.8.1):
@epicgamer300065,这里有一个实际的完全空闲解决方案,在win10pro上使用Python 3.8.1对我有效,但在terminal中不起作用 由于您的访问权限有限,我在此提供了所需的完整模块
idlecolors.py
,以方便您复制/粘贴,避免无法安装
如您所见,唯一的依赖项是模块sys
和random
,但是random
只需要randcol()
函数,如果必须的话,您可以不使用它
下面是idlecolors.py
:
import sys
import random
# This will only work in IDLE, it won't work from a command prompt
try:
shell_connect = sys.stdout.shell
except AttributeError:
print("idlecolors highlighting only works with IDLE")
exit()
# Map the colour strings to IDLE highlighting
USE_CUSTOM_COLORS = False # Change to True if you want to use custom colours
global colormap
if USE_CUSTOM_COLORS:
colormap = {"red": "COMMENT",
"orange": "KEYWORD",
"green": "STRING",
"blue": "stdout",
"purple": "BUILTIN",
"black": "SYNC",
"brown": "console",
"user1": "DEFINITION",
"user2": "sel",
"user3": "hit",
"user4": "ERROR",
"user5": "stderr"}
else:
colormap = {"red": "COMMENT",
"orange": "KEYWORD",
"green": "STRING",
"blue": "stdout",
"purple": "BUILTIN",
"black": "SYNC",
"brown": "console"}
# ---------------------------
# Functions
# ---------------------------
# Like the print() function but will allow you to print colours
def printc(text, end="\n"):
# Parse the text provided to find {text:color} and replace with the colour. Any text not encompassed in braces
# will be printed as black by default.
buff = ""
for char in text:
if char == "{":
# Write current buffer in black and clear
shell_connect.write(buff, colormap["black"])
buff = ""
elif char == "}":
# Write current buffer in color specified and clear
tag_write = buff.split(":")
shell_connect.write(tag_write[0], tag_write[1])
buff = ""
else:
# Add this char to the buffer
buff += char
# Write the chosen end character (defaults to newline like print)
sys.stdout.write( end )
# Individual colour functions
def red(text):
return "{"+ text + ":" + colormap["red"] + "}"
def orange(text):
return "{"+ text + ":" + colormap["orange"] + "}"
def green(text):
return "{"+ text + ":" + colormap["green"] + "}"
def blue(text):
return "{"+ text + ":" + colormap["blue"] + "}"
def purple(text):
return "{"+ text + ":" + colormap["purple"] + "}"
def black(text):
return "{"+ text + ":" + colormap["black"] + "}"
def brown(text):
return "{"+ text + ":" + colormap["brown"] + "}"
def randcol(text):
color = random.choice(list(colormap.keys()))
return "{"+ text + ":" + colormap[color] + "}"
# User defined colours
def user1(text):
return "{"+ text + ":" + colormap["user1"] + "}"
def user2(text):
return "{"+ text + ":" + colormap["user2"] + "}"
def user3(text):
return "{"+ text + ":" + colormap["user3"] + "}"
def user4(text):
return "{"+ text + ":" + colormap["user4"] + "}"
def user5(text):
return "{"+ text + ":" + colormap["user5"] + "}"
下面是您将如何使用它:
from idlecolors import *
printc( red("Red text") )
printc( "If you add " + red("red") + " to " + blue("blue") + ", you get " + purple("purple") )
# Print a line in a random colour
printc( randcol("This is a random colour") )
# Print each word in a random colour
mytext = "This is a random piece of text which I want to print in random colours"
mytext = mytext.split(" ")
for word in mytext:
printc(randcol(word), end=" ")
可用的颜色有
红色()
,橙色()
,绿色()
,蓝色()
,紫色()
,黑色()
,棕色()
,您可以使用随机颜色来选择此选项。这是否回答了您的问题?你对“外部模块”有什么不满?它们中的大多数只是用Python编写的函数,因此在很大程度上,您可以说“是的,这在没有外部代码的情况下是可能的”。这样的模块并没有添加一层未知的魔力,比如说,向控制台发送普通的ANSI转义码——这确实可以“手动”完成。但是为什么要避免它们呢?如果您询问如何在系统终端/控制台中更改颜色,那么应该删除python idle标记。如果您在空闲状态下运行代码并将其打印到外壳中时询问如何更改颜色,则会出现另一个副本,当前的官方答案是您不能更改颜色,因此如果可以,它将受到限制并可能更改。@usr2564301,因为我只能访问学校中的默认模块并下载新模块(例如pygame)是一种痛苦,因此避免它们可以节省很多时间。@Brian很遗憾,这并不能回答我的问题,因为python 3.7.2返回(在我的例子中)[7;30;40[0m[7;30;41m7;30;41[0m[7;30;42m7;30;42[0m[7;30;43m7;30;43[7;30;44m7;30;44[0m[7;30;45[7;30;30;46m7;30;46[7;30;40[7;47;40m7;30;40m[7][7;31;41M7;31;41[0m[7;31;42M7;31;42[0m[7;31;43M7;31;43[0m[7;31;44M7;31;44[0m[7;31;31;45[0m[7;31;46M7;31;46[0m[7;31;47M7;31;47[0m[7;32;40M7;32;40[0m[7;32;32;41M7;32;41[0m[7;32;42[7;42;42[7;32;43;43M7;32;447;447][7;32;45m 7;32;45
感谢您的回答,但正如我之前所解释的,我无法安装其他并非python 3.7.2默认附带的模块。colorama就是其中之一。
from idlecolors import *
printc( red("Red text") )
printc( "If you add " + red("red") + " to " + blue("blue") + ", you get " + purple("purple") )
# Print a line in a random colour
printc( randcol("This is a random colour") )
# Print each word in a random colour
mytext = "This is a random piece of text which I want to print in random colours"
mytext = mytext.split(" ")
for word in mytext:
printc(randcol(word), end=" ")