Python Spyder initializing Hello World Kivi app once?(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.
这篇关于Python Spyder 初始化 Hello World Kivi 应用程序一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python Spyder 初始化 Hello World Kivi 应用程序一次?
基础教程推荐
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
