如何在Kivy应用程序退出时运行方法

如何在Kivy应用程序退出时运行方法,第1张

如何在Kivy应用程序退出时运行方法

如果您希望您的应用程序在GUI关闭后直接运行,那么最简单,最小的方法就是将所有退出代码放置在之后

TestApp().run()
run()
创建一个无限循环,该循环还会清除kivy中的所有事件数据,因此不会挂起。window
/ gui实例死亡后,该无限循环就会中断。因此,之后的任何代码也将仅在GUI死后执行。

如果要通过关闭套接字事件或d出窗口询问用户是否确实是他们真正想做的事情来创建GUI的正常关闭方式,那么可以为on_request_close事件创建一个钩子:

from kivy.config import ConfigConfig.set('kivy', 'exit_on_escape', '0')from kivy.app import Appfrom kivy.uix.label import Labelfrom kivy.uix.boxlayout import BoxLayoutfrom kivy.uix.button import Buttonfrom kivy.uix.popup import Popupfrom kivy.core.window import Windowclass ChildApp(App):    def build(self):        Window.bind(on_request_close=self.on_request_close)        return Label(text='Child')    def on_request_close(self, *args):        self.textpopup(title='Exit', text='Are you sure?')        return True    def textpopup(self, title='', text=''):        """Open the pop-up with the name.        :param title: title of the pop-up to open        :type title: str        :param text: main text of the pop-up to open        :type text: str        :rtype: None        """        box = BoxLayout(orientation='vertical')        box.add_widget(Label(text=text))        mybutton = Button(text='OK', size_hint=(1, 0.25))        box.add_widget(mybutton)        popup = Popup(title=title, content=box, size_hint=(None, None), size=(600, 300))        mybutton.bind(on_release=self.stop)        popup.open()if __name__ == '__main__':    ChildApp().run()

由pythonic64提供,他早在发行时就在该主题上创建了要点。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5508288.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存