如何防止在kivy应用程序中关闭x上的窗口

时间:2019-03-18 00:04:58

标签: python kivy

是否可以通过单击右上角的“ x”直到满足特定条件来防止关闭奇异果窗口?

1 个答案:

答案 0 :(得分:0)

您可以通过将窗口的on_request_close绑定到一个函数来检查是否满足条件来做到这一点:

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()