【问题标题】:How to display a popup when closing a Kivy app关闭 Kivy 应用程序时如何显示弹出窗口
【发布时间】:2017-07-31 23:24:15
【问题描述】:

我想显示一个弹出窗口,确认用户真的想在应用程序正常关闭时关闭它。

我想在 Android 和 Python 2.7 上执行此操作

【问题讨论】:

    标签: python python-2.7 kivy


    【解决方案1】:

    app.stop() 函数实际上是在退出 Kivy 应用程序时调用的。 随心所欲地自定义此功能:

    class MyApp(App):
    
        def stop(self, *largs):
            # Open the popup you want to open and declare callback if user pressed `Yes`
            popup = ExitPopup(title="Are you sure?")
            popup.bind(on_confirm=partial(self.close_app, *largs))
            popup.open()
    
        def close_app(self, *largs):   
            super(MyApp, self).stop(*largs)
    
    class ExitPopup(Popup):
    
        def __init__(self, **kwargs):
            super(ExitPopup, self).__init__(**kwargs)
            self.register_event_type('on_confirm')
    
        def on_confirm(self)
            pass
    
        def on_button_yes(self)
            self.dispatch('on_confirm')
    

    在kv文件中,将Yes按钮的on_release方法绑定到on_button_yes函数。 如果该按钮被按下,on_button_yes() 将被调用,因此on_confirm 事件将被调度,应用程序将被关闭。

    【讨论】:

    • 可以考虑使用app.on_stop()
    猜你喜欢
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    相关资源
    最近更新 更多