更改shell打印颜色在python 3.3.2 [英] Change shell print color in python 3.3.2

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

问题描述

在Linux和Windows shell中,有没有办法更改python 3.3.2中的shell打印颜色?我已经阅读所有其他主题,但没有人使用此版本或我应该导入一个模块。例如,我想打印这样与纯python模块:





。我使用他们两个!我说没有人在python 3.3上工作:
http: //stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-pytho n



请不要说一个重复的问题,因为没有这些答案在Python 3.3.2工作,我想使用一个默认模块,不是colorama,我应该安装在python!

解决方案

要更改颜色方案,请编辑 config.txt 中的[Colors]部分。然而,你不能在程序期间这样做 - 即使你可能会令人厌恶的低效率。您不需要安装任何新模块。你可以通过 subprocess 这个Python自带的模块来实现。



类似于:

 从子进程导入调用

call('color a',shell = True)#this设置颜色为浅绿色
print('快速的棕色狐狸跳过懒惰的狗'。)

这适用于Windows,您可以根据操作系统轻松更改调用的命令。对于你的程序,你可以把颜色放在循环中,改变 print 语句中的消息。 / p>

注意,这仅在从文件位置或从命令行运行它时才有效。当它在IDLE中运行时,它不工作。希望我帮助了!



编辑:您可以找到颜色列表此处。语法是: color'background ID''text ID'



这将允许你做:

 导入时间
从子进程导入调用

颜色('a' ,'e','c'):#循环通过不同的颜色
call('cls',shell = True)#clears屏幕
call('color'+ color,shell = True)$
time.sleep(1)

输入(\\\
Press输入退出。)

从中可以修改代码以使用您选择的颜色。不幸的是,绝对没有办法没有所有的颜色在屏幕上同时。因此, DO 需要外部模块,恐怕。


Is there any way to change shell print color in python 3.3.2 in Linux and Windows shell? I've read all other topics but none of them working with this version or I should import a module. For example I want to print like this with pure python modules:

. I use both of them! And I said none of them working on python 3.3 even: http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python

And please don't say it's a duplicate question because none of these answers working in python 3.3.2 and I wanted to use a default module to do that not colorama which I should install on python!

解决方案

To change the color scheme, edit the [Colors] section in config.txt. However, you cannot do this during a program- and even if you could it would be disgustingly inefficient. You need not install any new modules. You can do this through subprocess, a module that comes with Python.

Something like:

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.')

This works for Windows, and you can easily change the command that is called depending on the operating system. For your program, you could put the colours in a for loop, changing with the messages in the print statement.

As a note, this only works if you run it from its file location or from the command line. It will not work when you run it in IDLE. Hope I helped!

EDIT: You can find a list of colours here. The syntax is: color 'background ID''text ID'.

This would allow you to do something like:

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. ")

From that, you can modify the code to use colours of your choice. Unfortunately, there is absolutely no way no have all the colors on the screen at the same time. For that you DO need external modules, I'm afraid.

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

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