在python 3.3.2中更改外壳打印颜色
问题内容:
有什么办法可以在Linux和Windows Shell中更改python
3.3.2中的shell打印颜色?我已经阅读了所有其他主题,但是没有一个主题与此版本兼容,否则我应该导入一个模块。例如,我想使用纯python模块进行这样的打印:
。我都用!我什至说他们都不在python
3.3上工作:https : //stackoverflow.com/questions/287871/print-in-
terminal-with-colors-using-
pytho n
而且请不要说这是一个重复的问题,因为这些答案都无法在python
3.3.2中工作,而且我想使用默认模块来实现,而不是我应该在python上安装的colorama!
问题答案:
要更改配色方案,请在中编辑[颜色]部分config.txt
。但是,您无法在程序执行期间执行此操作,即使您这样做,效率也非常低。您无需安装任何新模块。您可以通过subprocess
Python附带的模块来执行此操作。
就像是:
from subprocess import call
call('color a', shell=True) #this sets the color to light green
print('The quick brown fox jumps over the lazy dog.')
这适用于Windows,并且您可以根据操作系统轻松更改所调用的命令。对于您的程序,您可以将颜色for
循环放置,并根据print
语句中的消息进行更改。
请注意,只有从文件位置或命令行运行它时,此方法才有效。当您在IDLE中运行它时,它将不起作用。希望我能帮上忙!
编辑: 您可以在这里找到颜色列表。语法为:color 'background ID''text ID'
。
这将使您可以执行以下操作:
import time
from subprocess import call
for color in('a', 'e', 'c'): #cycles through different colours
call('cls', shell=True) #clears the screen
call('color ' + color, shell=True)
print('The quick brown fox jumps over the lazy dog.')
time.sleep(1)
input("\nPress enter to exit. ")
由此,您可以修改代码以使用您选择的颜色。不幸的是,绝对不可能同时在屏幕上显示所有颜色。对于您 不要 需要外部模块,我害怕。