Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Spyder initializing Hello World Kivi app once?

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

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()
like image 614
Mauricio Maroto Avatar asked Jul 10 '16 05:07

Mauricio Maroto


People also ask

How to print Hello World in Kivy?

class MyFirstKivyApp(App): def build(self): return Label(text ="Hello World !")

Can you use KIVY in Python?

Build and distribute beautiful Python cross-platform GUI apps with ease. Kivy runs on Android, iOS, Linux, macOS and Windows. Get started! Kivy has been built to be easy to use, cross-platform and fast.

What is the purpose of KIVY?

Kivy helps us to design innovative user interfaces with multi-touch functionalities. It can smoothly work with various platforms such as Windows, Android, Linux, iOs, macOS, and Raspberry Pi. It helps us to run code on all supported platforms. It provides well-documented APIs.


3 Answers

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.

like image 65
MaThMaX Avatar answered Oct 18 '22 20:10

MaThMaX


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.

like image 21
phchen2 Avatar answered Oct 18 '22 21:10

phchen2


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

Part 2

Part 3

like image 1
Rafsan Uddin Beg Rizan Avatar answered Oct 18 '22 20:10

Rafsan Uddin Beg Rizan