<bdo id='lMXtz'></bdo><ul id='lMXtz'></ul>
  • <tfoot id='lMXtz'></tfoot>

    <small id='lMXtz'></small><noframes id='lMXtz'>

  • <i id='lMXtz'><tr id='lMXtz'><dt id='lMXtz'><q id='lMXtz'><span id='lMXtz'><b id='lMXtz'><form id='lMXtz'><ins id='lMXtz'></ins><ul id='lMXtz'></ul><sub id='lMXtz'></sub></form><legend id='lMXtz'></legend><bdo id='lMXtz'><pre id='lMXtz'><center id='lMXtz'></center></pre></bdo></b><th id='lMXtz'></th></span></q></dt></tr></i><div id='lMXtz'><tfoot id='lMXtz'></tfoot><dl id='lMXtz'><fieldset id='lMXtz'></fieldset></dl></div>
  • <legend id='lMXtz'><style id='lMXtz'><dir id='lMXtz'><q id='lMXtz'></q></dir></style></legend>

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

        时间:2023-10-11
      1. <legend id='VjIJP'><style id='VjIJP'><dir id='VjIJP'><q id='VjIJP'></q></dir></style></legend>

        <tfoot id='VjIJP'></tfoot>
          <i id='VjIJP'><tr id='VjIJP'><dt id='VjIJP'><q id='VjIJP'><span id='VjIJP'><b id='VjIJP'><form id='VjIJP'><ins id='VjIJP'></ins><ul id='VjIJP'></ul><sub id='VjIJP'></sub></form><legend id='VjIJP'></legend><bdo id='VjIJP'><pre id='VjIJP'><center id='VjIJP'></center></pre></bdo></b><th id='VjIJP'></th></span></q></dt></tr></i><div id='VjIJP'><tfoot id='VjIJP'></tfoot><dl id='VjIJP'><fieldset id='VjIJP'></fieldset></dl></div>
                <bdo id='VjIJP'></bdo><ul id='VjIJP'></ul>
                  <tbody id='VjIJP'></tbody>

                  <small id='VjIJP'></small><noframes id='VjIJP'>

                  本文介绍了如何在 kivy 应用程序退出时运行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想在用户尝试退出应用程序时运行一个方法,有点像您确定要退出"或您要保存文件"类型的消息,只要用户尝试退出点击窗口顶部的退出按钮退出

                  I would like to run a Method when the user tries to exit the app , kind of like a "are you sure you want to exit" or "Do you want to save the file" type of message whenever the user tries to exit by clicking the Exit button on top of the window

                  类似的东西on_quit: app.root.saveSession()

                  推荐答案

                  如果您希望您的应用程序在 GUI 关闭后简单地运行,最简单和最小的方法是在 TestApp().run().run() 创建了一个无限循环,它还会清除 kivy 中的任何事件数据,因此它不会挂起.一旦窗口/gui 实例死亡,那个无限循环就会中断.因此,之后的任何代码都只会在 GUI 死掉之后才会执行.

                  If you want your application to simply run things after the GUI has closed, the easiest and smallest approach would be to place any exit code after TestApp().run(). run() creates a endless loop which also clears any event-data from within kivy so it doesn't hang. That endless loop breaks as soon as the window/gui instance dies. So there for, any code after will execute only after the GUI dies too.

                  如果您想创建一个优雅的 GUI 关闭,例如套接字关闭事件或弹出窗口询问用户是否真的想要这样做,那么为 on_request_close 事件创建一个钩子是要走的路:

                  If you want to create a graceful shutdown of the GUI with for instance socket-closing events or a popup asking the user if that's what they really want to do, then creating a hook for the on_request_close event is the way to go:

                  from kivy.config import Config
                  Config.set('kivy', 'exit_on_escape', '0')
                  
                  from kivy.app import App
                  from kivy.uix.label import Label
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.uix.button import Button
                  from kivy.uix.popup import Popup
                  from kivy.core.window import Window
                  
                  
                  class 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="noreferrer">gist 回到 issue 时的主题.

                  Courtesy of pythonic64 who created a gist on the topic in a issue way back when.

                  这篇关于如何在 kivy 应用程序退出时运行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Kivy ObjectProperty 更新标签文本 下一篇:如何使用 Kivy 制作工具提示?

                  相关文章

                  最新文章

                  1. <legend id='Pe25u'><style id='Pe25u'><dir id='Pe25u'><q id='Pe25u'></q></dir></style></legend>
                  2. <small id='Pe25u'></small><noframes id='Pe25u'>

                  3. <tfoot id='Pe25u'></tfoot>
                      <bdo id='Pe25u'></bdo><ul id='Pe25u'></ul>

                      <i id='Pe25u'><tr id='Pe25u'><dt id='Pe25u'><q id='Pe25u'><span id='Pe25u'><b id='Pe25u'><form id='Pe25u'><ins id='Pe25u'></ins><ul id='Pe25u'></ul><sub id='Pe25u'></sub></form><legend id='Pe25u'></legend><bdo id='Pe25u'><pre id='Pe25u'><center id='Pe25u'></center></pre></bdo></b><th id='Pe25u'></th></span></q></dt></tr></i><div id='Pe25u'><tfoot id='Pe25u'></tfoot><dl id='Pe25u'><fieldset id='Pe25u'></fieldset></dl></div>