Josip Maričević
Josip Maričević

Reputation: 407

How to prevent closing window on x in kivy app

Is there a way to prevent closing kivy window by clicking 'x' in top right corner until a certain condition is met?

Upvotes: 5

Views: 1568

Answers (1)

noEmbryo
noEmbryo

Reputation: 2231

You can do it by binding the window's on_request_close with a function to check if the conditions are met:

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.label import Label


class Base(Label):
    def __init__(self, **kwargs):
        super(Base, self).__init__(**kwargs)
        Window.bind(on_request_close=self.exit_check)
        self.counter = 0
        self.text = str(self.counter)

    def exit_check(self, *args):
        self.counter += 1
        if self.counter < 5:
            self.text = str(self.counter)
            return True  # block app's exit
        else:
            return False  # let the app close


class SampleApp(App):
    def build(self):
        return Base()


if __name__ == "__main__":
    SampleApp().run()

Upvotes: 7

Related Questions