Python Spyder初始化Hello World Kivi应用程序一次?

时间:2016-07-10 05:47:58

标签: python python-2.7 kivy spyder

有谁知道为什么Python的2.7 Spyder成功初始化了'Hello World' Kivy应用只需一次,即点击F5会带来窗口应用程序,但当我关闭它并再次点击F5时,它会显示以下错误:

[INFO              ] [Base        ] Start application main loop
[ERROR             ] [Base        ] No event listeners have been created
[ERROR             ] [Base        ] Application will leave

但是,通过Anacondas命令提示符初始化时没有错误。

这里是代码(与网站相同):

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()

3 个答案:

答案 0 :(得分:8)

实际上,示例程序只是您尝试以这种简单方式创建交互式UI的最小结构。

而在TestApp中,它实际上没有实现event listerners来处理关闭事件。当你创建你的实际项目时,你应该总是照顾它。如果您仔细查看logging,您会注意到当您关闭TestApp时错误已经发生,而不是当您“重新启动”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

因此,对于您的情况,一个简单的解决方法就是在Run->Configure面板中转到Console,而不是选择Execute in current Python or IPython console,您只需选择第二个选项,Execute in a new dedicated Python console。在这种情况下,在完成代码的时间,Python将关闭当前内核。无论何时运行代码,Spyder都会自动为此特定脚本创建一个新的专用内核。

答案 1 :(得分:3)

请参阅网页https://groups.google.com/forum/m/#!topic/kivy-users/yfhH7skAEJA,它提供了解决此问题的解决方案,我将代码重写如下,

from kivy.app import App
from kivy.uix.button import Button

class TestApp(App):
   def build(self):
      return Button(text='Hello World')

def reset():
import kivy.core.window as window
from kivy.base import EventLoop
if not EventLoop.event_listeners:
    from kivy.cache import Cache
    window.Window = window.core_select_lib('window', window.window_impl, True)
    Cache.print_usage()
    for cat in Cache._categories:
        Cache._objects[cat] = {}

if __name__ == '__main__':
   reset()
   TestApp().run()

reset()函数将清除Window的状态并正常运行TestApp()。

答案 2 :(得分:-1)

用于外部系统终端将解决此问题。 Part 1

Part 2

Part 3