Python 使用退出箭头防止Android上的kivy应用程序退出

Python 使用退出箭头防止Android上的kivy应用程序退出,python,kivy,Python,Kivy,我想用退出箭头阻止Android上的kivy应用程序退出 当我在config.ini文件中写入exit\u on\u escape=0时,它不会改变任何内容(但是在WINDOWS 8上,使用Esc键时,此操作有效) 我还尝试在请求关闭时拦截,但未成功 import kivy kivy.require('1.0.8') from kivy.core.window import WindowBase from kivy.uix.widget import Widget from kivy.app

我想用退出箭头阻止Android上的kivy应用程序退出

当我在
config.ini
文件中写入
exit\u on\u escape=0
时,它不会改变任何内容(但是在WINDOWS 8上,使用Esc键时,此操作有效)

我还尝试在请求关闭时拦截
,但未成功

import kivy
kivy.require('1.0.8')

from kivy.core.window import WindowBase
from kivy.uix.widget import Widget 
from kivy.app import App
from kivy.clock import Clock

class Test(Widget):

    def __init__(self, **kwargs):
        super(Test,self).__init__(**kwargs)     

class TestApp(App):
    def build(self):

    Clock.schedule_once(self.my_callback, 0)
    return Test()

def my_callback(self,*dt):
    print("mycallback")
    win=self._app_window
    win.fullscreen=1 #OK 
    win.bind(on_request_close=self.alwaystrue)

def alwaystrue(*largs, **kwargs) :
    print("alwaystrue")#never happens ...
    return True
if __name__ == '__main__':
    TestApp().run()

在应用程序类中执行以下操作:

from kivy.app import App
from kivy.core.window import Window

class MyApp(App):
    def build(self):
        self.bind(on_start=self.post_build_init)
        # do all your normal stuff as well
        return whatever

    def post_build_init(self,ev):
        if platform() == 'android':
            import android
            android.map_key(android.KEYCODE_BACK, 1001)

        win = Window
        win.bind(on_keyboard=self.key_handler)

    def key_handler(self, window, keycode1, keycode2, text, modifiers):
        if keycode1 == 27 or keycode1 == 1001:
            # Do whatever you want here - or nothing at all
            # Returning True will eat the keypress
            return True
        return False

这可能不是所有必要的(使用中间的
post\u build\u init()
方法和
android.map\u key()
东西),但我最初是从邮件中获得的,我想我没有更新版本。无论如何,它对我来说是有效的。

import kivy
语句下面添加以下代码片段

from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')

下面是一个工作示例,如何在请求关闭时使用

该行:

Config.set('kivy', 'exit_on_escape', '0')
禁用Esc-按钮

from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.core.window import Window


class ChildApp(App):

    def build(self):
        Window.bind(on_request_close=self.on_request_close)
        return Label(text='Child')

    def on_request_close(self, *args):
        self.textpopup(title='Exit', text='Are you sure?')
        return True

    def textpopup(self, title='', text=''):
        """Open the pop-up with the name.

        :param title: title of the pop-up to open
        :type title: str
        :param text: main text of the pop-up to open
        :type text: str
        :rtype: None
        """
        box = BoxLayout(orientation='vertical')
        box.add_widget(Label(text=text))
        mybutton = Button(text='OK', size_hint=(1, 0.25))
        box.add_widget(mybutton)
        popup = Popup(title=title, content=box, size_hint=(None, None), size=(600, 300))
        mybutton.bind(on_release=self.stop)
        popup.open()


if __name__ == '__main__':
    ChildApp().run()