Python Spyder initializing Hello World Kivi app once?

前端 未结 3 1417
攒了一身酷
攒了一身酷 2020-12-19 05:48

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

相关标签:
3条回答
  • 2020-12-19 06:14

    For use in external system terminal will solve this. Part 1

    Part 2

    Part 3

    0 讨论(0)
  • 2020-12-19 06:21

    Refer to the webpage: https://groups.google.com/forum/m/#!topic/kivy-users/yfhH7skAEJA, it gave a solution for fixing this issue, I rewrite the code as below,

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

    The reset() function will clean up the Window's state and run the TestApp() normally.

    0 讨论(0)
  • 2020-12-19 06:36

    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.

    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
    

    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.

    0 讨论(0)
提交回复
热议问题