pythonkivypython-3.4quit

How to hide/disable close button or at least catch/override built-in quit() function? (Kivy)


I need to do some actions before quit (save settings, etc.), I do it with Button widgets, but if I press on close button of the window, it just closes. Maybe I should handle App's on_close() event somehow? Then I don't know how to send data from Layout to App. For now I have smth like:

def quit_game(self):
        # Saving different data.
        ...
        quit()

What should I do?


Solution

  • Yes, you should handle the on_stop method on your App class by implementing the appropriate method.

    Example:

    class MyApp(App):
        ...
        def on_stop(self):
            # do what you need to here
            value_to_save = self.root.subwidget.value
            # self.root is your root widget - either the 
            # widget you return from App.build() or
            # the root widget in your app kv file
    

    Keep in mind, also, that if you plan to develop for Android you will need to implement on_pause as well. After an Android app is paused, it can be killed without warning by the OS and your on_stop method will not be called.