Greg
Greg

Reputation: 101

Python / Kivy App running only once

I'm running a very simple python (3.5) script using Spyder IDE from Anaconda platform.

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput

class LoginScreen(GridLayout):
    def __init__(self, **kwargs):
        super(LoginScreen,self).__init__(*kwargs)
        self.cols = 2
        self.add_widget(Label(text='Username'))
        self.username = TextInput(multiline=False)
        self.add_widget(self.username)
    
        self.add_widget(Label(text='Password'))
        self.password = TextInput(multiline=False, password=True)
        self.add_widget(self.password)

class TestApp(App):
    def build(self):
        return LoginScreen()

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

"App" runs fine on the first try (it doesn't do anything yet, just launches), but when I'm trying to launch it again, I'm getting a following error message:

[INFO ] [Base ] Start application main loop

[ERROR ] [Base ] No event listeners have been created

[ERROR ] [Base ] Application will leave

The only way to make it work again is to restart the kernel running in Spyder. After that the app will launch once again (but only once).

What I've noticed is that on the first run, the Ipython console would print a lot of other info first, before the "Start application main loop" line, and on those failed runs, I'm getting just those 3 lines above.

Does anyone know what I'm doing wrong? Many thanks for any help.

Upvotes: 1

Views: 3446

Answers (1)

Peter Badida
Peter Badida

Reputation: 12199

Not that much Kivy-related problem. Spyder probably keeps old stuff in memory? Try in a simple interpreter type your code line by line. Then close the app and try again typing TestApp().run() - the same three lines.

This way you'll see that if the old stuff (variables, classes, whatever is still accessible) is present, Kivy won't allow you to launch the app (still a mistery to me, maybe because of window-related code?)

If there is something in Spyder to flush old stuff from memory, then set it after each Kivy exit and you should be good to go.

Upvotes: 2

Related Questions