Python Spyder 初始化 Hello World Kivi 应用程序一次?
问题描述
有谁知道为什么 Python 的 2.7 Spyder 只成功初始化 'Hello World' Kivy 应用程序一次,即按 F5 会带来窗口应用程序,但是当我关闭它并再次按 F5 时,它会显示以下错误:
Does anyone know why Python's 2.7 Spyder is successfully initializing the 'Hello World' Kivy app just once, i.e. hitting F5 brings the window app, but when I close it and hit F5 again, it says the following error:
[INFO ] [Base ] Start application main loop
[ERROR ] [Base ] No event listeners have been created
[ERROR ] [Base ] Application will leave
但是,通过 Anacondas 命令提示符初始化时没有错误.
However, there is no error when initialized through Anacondas Command Prompt.
这是代码(与网站相同):
Here's the code (same as website):
from kivy.app import App
from kivy.uix.button import Button
class TestApp(App):
def build(self):
return Button(text='Hello World')
TestApp().run()
if __name__ == '__main__':
TestApp().run()
解决方案
其实这个示例程序只是一个最小的结构,让你尝试如何以如此简单的方式创建交互式 UI.
Actually the sample program is just a minimum structure for you to try out how the interactive UI can be created in such a simple way.
而在TestApp
中,它实际上并没有实现event listeners
来处理close事件.当您创建实际项目时,您应该始终注意这一点.实际上,如果您仔细查看 logging
,您会注意到错误已经在您关闭 TestApp
时发生,而不是在您重新启动"您 TestApp
:
And the in TestApp
, it actually didn't implment the event listerners
to handle the close event. And when you create your actual project, you should always take care of that. Acually if you look at the logging
carefully, you would notice that the error happens already when you close the TestApp
, not when you "re-start" you TestApp
:
[INFO ] [Base ] Leaving application in progress...
INFO:kivy:[Base ] Leaving application in progress...
[INFO ] [Base ] Start application main loop
INFO:kivy:[Base ] Start application main loop
[ERROR ] [Base ] No event listeners have been created
ERROR:kivy:[Base ] No event listeners have been created
[ERROR ] [Base ] Application will leave
ERROR:kivy:[Base ] Application will leave
因此,对于您的情况,一个简单的解决方法是在 Console
面板中转到 Run->Configure
,而不是选择 Execute in current Python or IPython console
,你只需选择第二个选项,即Execute in a new dedicated Python console
.在这种情况下,当您完成代码时,Python 将关闭当前内核.并且每当你运行你的代码时,Spyder
会自动为这个特定的脚本创建一个新的专用内核.
So for your case, the one simple work-around is that you go to Run->Configure
, in the Console
panel, instead of you choose to Execute in current Python or IPython console
, you just choose the second option, which is Execute in a new dedicated Python console
. In this case, where time your finished the code, the Python will close the current kernel. And whenever you run your code, Spyder
will automatically create a new dedicated kernel for this particular script.
相关文章