简体   繁体   中英

How to prevent closing window on x in kivy app

有没有一种方法可以通过单击右上角的“ x”直到满足特定条件来防止关闭kivy窗口?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM