import-win32consolenn#-获取控制台句柄nhconsole-=-win32consoleGetStdHandlewin32consoleSTD_OUTPUT_HANDLEnn#-设置文本颜色nwin32consoleSetConsoleTextAttributehconsole-win32consoleFOREGROUND_REDnn#-写屏nprint我爱你nn报错:module-win32console-has-no-attribute-SetConsoleTextAtt
这个错误可能是因为您的计算机上没有安装 pywin32 模块,或者您的 pywin32 模块版本太低。请尝试更新 pywin32 模块,或者安装最新版本的 pywin32 模块。您可以使用以下命令在终端中安装最新版本的 pywin32 模块:
pip install pywin32
如果您已经安装了 pywin32 模块,请尝试使用以下代码替换您的代码:
import ctypes
# 获取控制台句柄
STD_OUTPUT_HANDLE = -11
hconsole = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
# 设置文本颜色
FOREGROUND_RED = 0x0c
ctypes.windll.kernel32.SetConsoleTextAttribute(hconsole, FOREGROUND_RED)
# 写屏
print("我爱你")
这个代码使用 ctypes 模块来实现控制台文本颜色的更改。
原文地址: https://www.cveoy.top/t/topic/p04 著作权归作者所有。请勿转载和采集!