【发布时间】:2014-05-22 01:56:37
【问题描述】:
我正在尝试让我一直在构建的应用程序在某个时间点停止,并在停止时运行清理程序。这看起来应该很容易,但我一直遇到错误,我一直无法找到解决方案。
我使用 kivy 1.8.0 和 Python 3.3。为方便起见,我修改了 kivy 文档中的一些代码,因为我的代码基于相同的框架,并且都给了我完全相同的错误:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
# Create both screens. Please note the root.manager.current: this is how
# you can control the ScreenManager from kv. Each screen has by default a
# property manager that gives you the instance of the ScreenManager used.
Builder.load_string("""
<MenuScreen>:
BoxLayout:
Button:
text: 'Goto settings'
on_press: root.manager.current = 'settings'
Button:
text: 'Quit'
on_press: root.exit()
<SettingsScreen>:
BoxLayout:
Button:
text: 'My settings button'
Button:
text: 'Back to menu'
on_press: root.manager.current = 'menu'
""")
# Declare both screens
class MenuScreen(Screen):
def exit(self):
App.stop(self)
class SettingsScreen(Screen):
pass
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))
class TestApp(App):
def build(self):
return sm
def on_stop(self):
print('leaving now') # this is where I'd want to run the end of program procedure
if __name__ == '__main__':
TestApp().run()
当我运行它并单击Quit 按钮时,我收到以下错误:
builtins.KeyError: 'on_stop'
我还要注意,如果我注释掉class TestApp 中的on_stop 函数,错误仍然存在。知道出了什么问题吗?
【问题讨论】: