栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

如果要通过关闭套接字事件或弹出窗口询问用户是否确实是他们真正想做的事情来创建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提供,他早在发行时就在该主题上创建了要点。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517053.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号