使用ESCAPE ARROW防止在Android上退出kivy应用程序

时间:2014-06-20 21:25:37

标签: python kivy

我想阻止使用ESCAPE ARROW在Android上退出kivy应用程序。

当我在exit_on_escape = 0文件中写config.ini时,它不会改变任何东西(但是在WINDOWS 8上, Esc 键有效)。

我也尝试拦截on_request_close

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

3 个答案:

答案 0 :(得分:3)

在App类中执行以下操作:

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_build_init()方法和android.map_key()的方法),但我最初是从邮寄的帖子中得到的,我不认为我有更新后的版本。无论如何,它对我有用。

答案 1 :(得分:1)

import kivy声明

下方添加以下代码段
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')

答案 2 :(得分:1)

这是一个工作示例,如何使用on_request_close。

该行:

Config.set('kivy', 'exit_on_escape', '0')

禁用Esc -button。

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