我有一个奇怪的问题,我不能把手指放在上面。有一个程序,我使用(并不时提供)彩色控制台输出。在重新安装Windows之前,一切都很顺利。现在我无法获得彩色输出。
这是用于着色的script。
我已经设法将问题缩小到或多或少,简单的情况,但我不知道出了什么问题。
这是按预期工作的控制台提示符(字符串test
以红色打印):
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.insert(0, r'c:\bin\SV\tea\src')
>>> from tea.console.color import cprint, Color
>>> cprint('test\n', Color.red)
test
>>>
但是当我使用相同版本的python运行以下脚本时,我得到输出test
但不是红色(没有颜色,只是默认的控制台颜色):
import sys
sys.path.insert(0, r'c:\bin\SV\tea\src')
from tea.console.color import cprint, Color
cprint('test\n', Color.red)
c:\Python27\python.exe
)或py -2
,
但它没有帮助。 任何想法我可以尝试做些什么?
修改:
也许不清楚,但我用来着色输出的脚本是在相关链接中给出的。这是再一次: https://bitbucket.org/alefnula/tea/src/dc14009a19d66f92463549332a321b29c71d47b8/src/tea/console/color.py?at=default
答案 0 :(得分:4)
我找到了问题和解决方案。
我认为问题是x64 ctypes模块中的错误。我安装了Python 2.7 x64并且跟随该版本(来自我所链接的脚本):
ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, code)
返回错误代码6,其中包含说明The handle is invalid
。经过一番调查,我推断出问题可能是x64版本的python,所以我安装了32位版本,一切都按预期工作。
由于这解决了我的问题,而且我没有时间进行更深入的分析,我会将其留在此处,只是想提出某种解决方案。