Python/Kivy App只运行一次 [英] Python / Kivy App running only once
问题描述
我正在使用Anaconda平台上的Spyder IDE运行一个非常简单的python(3.5)脚本.
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]启动应用程序主循环
[INFO ] [Base ] Start application main loop
[错误] [基本]尚未创建事件侦听器
[ERROR ] [Base ] No event listeners have been created
[错误] [基本]应用程序将退出
[ERROR ] [Base ] Application will leave
使其再次起作用的唯一方法是重新启动在Spyder中运行的内核.之后,该应用将再次启动(但仅启动一次).
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).
我注意到的是,在第一次运行时,Ipython控制台会在启动应用程序主循环"之前先打印很多其他信息.行,在那些失败的运行中,我只得到上面的三行.
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.
推荐答案
与Kivy相关的问题不多. Spyder可能在内存中保留了旧内容?尝试使用一个简单的解释器,逐行键入您的代码.然后关闭应用程序,然后再次尝试输入TestApp().run()
-同样的三行.
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.
这样,您会看到,如果存在旧的内容(变量,类,仍然可以访问的任何内容),Kivy将不允许您启动该应用程序(对我来说仍然很迷茫,可能是由于与窗口相关代码?)
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?)
如果Spyder中有某些东西可以清除内存中的旧内容,则在每次Kivy退出后进行设置,您应该一切顺利.
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.
这篇关于Python/Kivy App只运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!