Python Spyder一次初始化Hello World Kivi应用程序吗? [英] Python Spyder initializing Hello World Kivi app once?

查看:251
本文介绍了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 listerners处理关闭事件.在创建实际项目时,应始终注意这一点.如果您仔细地查看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,而是选择第二个选项,即.在这种情况下,当您完成代码的时间之后,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.

这篇关于Python Spyder一次初始化Hello World Kivi应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆