首页 > 解决方案 > 运行curses.initscr()时出现错误的诅咒-python

问题描述

背景:

每当我s = curses.initscr()在 IDLE 中运行时,都会收到以下错误消息:

  File "C:/Users/jacob/AppData/Local/Programs/Python/Python37-32/screentest.py", line 3, in <module>
    s = curses.initscr()
  File "C:\Users\jacob\AppData\Local\Programs\Python\Python37-32\lib\curses\__init__.py", line 30, in initscr
    fd=_sys.__stdout__.fileno())
AttributeError: 'NoneType' object has no attribute 'fileno'

这是来自 PyCharm 的消息:


Redirection is not supported.

Process finished with exit code 1

当我从 DevDungeon 运行一个示例片段时,


print("Preparing to initialize screen...")
screen = curses.initscr()
print("Screen initialized.")
screen.refresh()

curses.napms(2000)
curses.endwin()

print("Window ended.")

在启动 python 的命令提示符下,它只是给出了一个不可交互的空白屏幕。

shell中发生的事情是否正确?

这到底是怎么回事?

我怎样才能解决这个问题?

请帮忙谢谢

标签: python-3.xwindowspycharmpython-idlepython-curses

解决方案


sys.__stdout__是 python 进程的原始 sys.stdout。当您使用仅存在于 Windows 上的 pythonw.exe 启动 python 时,python 最初执行stdout = __stdout__ = None. (我不确定 *nix 上会发生什么。) pythonw.exe 用于在没有关联文本控制台的情况下使用 GUI UI 运行 python。在 Windows 上,IDLE 图标和开始菜单项使用 pythonw 运行 python。与其他 GUI IDE 相同。

curses使用文本终端或控制台运行。它假设sys.__stdout__是这样的。sys.__stdout__当为 None时它​​不能工作。

如果你从命令行终端/控制台启动 IDLE python -m idlelibsys.__stdout__将是那个终端,它会有一个 fileno()。之后会发生什么我不知道。如果您的程序使用 curses,您最好在文本控制台中运行它。


推荐阅读