退出包装器时,Python会诅咒TypeError [英] Python curses TypeError when exiting wrapper
问题描述
我正在运行Mac OS X 10.9.5,并用curses.wrapper
包装我的main()
函数时,我的程序成功退出后出现以下错误:
I'm running Mac OS X 10.9.5 and when wrapping my main()
function with curses.wrapper
I'm getting the following error after my program exits successfully:
Traceback (most recent call last):
File "test.py", line 42, in <module>
wrapper(main(SCREEN))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/curses/__init__.py", line 94, in wrapper
return func(stdscr, *args, **kwds)
TypeError: 'NoneType' object is not callable
一些放大代码:
if __name__ == "__main__":
# Initialize screen
SCREEN = curses.initscr()
# Run program with wrapper in case it fails
wrapper(main(SCREEN))
# Terminal cleanup
curses.nocbreak()
SCREEN.keypad(False)
curses.echo()
如果我使用CTRL + C
尝试在程序运行时退出该程序,则会显示异常,但终端仍处于混乱状态(包装器不起作用).我在这里想念一个明显的东西吗?
If I use CTRL + C
to attempt to exit the program while it's running, the exception is shown but the terminal remains in a disarrayed state (wrapper doesn't do it's job). Am I missing an something obvious here?
- 编辑*
我确认在通过远程SSH终端会话的Ubuntu 14.10服务器版本上也会发生这种情况.
I confirmed this also happens on Ubuntu 14.10 server edition over a remote SSH terminal session.
推荐答案
据我所知,您错误地调用了curses.wrapper
函数.
从文档:
As far as I can see, you're calling the curses.wrapper
function wrongly.
From the documentation:
curses.wrapper(func, ...)
初始化curses并调用另一个可调用对象func,该对象应该是使用curses的应用程序的其余部分. (...)然后将可调用对象func传递给主窗口"stdscr"作为其第一个参数,然后再传递给wrapper()的任何其他参数.
curses.wrapper(func, ...)
Initialize curses and call another callable object, func, which should be the rest of your curses-using application. (...) The callable object func is then passed the main window ‘stdscr’ as its first argument, followed by any other arguments passed to wrapper().
在您的示例中,它应如下所示:
In your example, it should look like this:
def main(SCREEN):
... # My program code
if __name__ == "__main__":
# The function main gets the stdscr passed by curses itself
wrapper(main)
如果您需要在主通话之前访问stdscr
在那种情况下,我不会使用包装器,而是使用 curses.endwin ()以取消初始化curses库. 未经测试的示例:
If you need to access to stdscr before the main call
I would not use wrapper in that case, but use curses.endwin() to deinitialize the curses library. Untested example:
SCREEN = curses.initscr()
# Modify your curses settings here
try:
main(SCREEN)
except: # End curses session before raising the error
curses.endwin()
raise
else: # End curses session if program terminates normally
curses.endwin()
这篇关于退出包装器时,Python会诅咒TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!