关闭Kivy应用程序时如何显示弹出窗口 [英] How to display a popup when closing a Kivy app

查看:653
本文介绍了关闭Kivy应用程序时如何显示弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个弹出窗口,确认用户确实希望在正常关闭该应用程序时将其关闭.

I would like to show a popup confirming the user would really like to close the application when it would normally close.

我想在Android和Python 2.7上做到这一点

I would like to do this on an Android and Python 2.7

推荐答案

app.stop()函数. 尽可能自定义此功能:

app.stop() function is actually called when exiting Kivy app. Customized this function as much as you like:

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事件,并且该应用程序将关闭.

In the kv file, bind on_release method of Yes button to the on_button_yes function. If that button is presses, on_button_yes() would be called, hence on_confirm event would be dispatched, and the app will be closed.

这篇关于关闭Kivy应用程序时如何显示弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆