在Kivy中实现android后退按钮功能

时间:2015-05-11 13:40:24

标签: kivy

首先,我已经通过很多例子,但无法弄清楚这一点,所以问这里。 我的应用程序将在Android上运行。屏幕1有一个按钮,点击后将进入屏幕2。 我需要的是在屏幕2上按下后退按钮时返回屏幕1的代码

我的代码:

#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')

from random import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from random import random
from random import choice
from kivy.properties import StringProperty
import time
from kivy.clock import Clock
from functools import partial
from kivy.utils import platform
from kivy.core.window import Window

Builder.load_string("""
<MenuScreen>:
    Button:
        text: "move to next screen 2"
        on_press: root.manager.current = 'game_mode'
<GameMode>:
    Label:
        text: "screen 2"
""")

class MenuScreen(Screen):
    pass

class GameMode(Screen):
    pass

sm = ScreenManager()
menu_screen = MenuScreen(name='menu')
sm.add_widget(menu_screen)
sm.add_widget(GameMode(name='game_mode'))

class TestApp(App):

    def build(self):
        self.bind(on_start=self.post_build_init)
        return sm

    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:
            sm.go_back()
            return True
        return False

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

请帮忙。我想要基于屏幕管理器的解决方案。如果您能改进我的代码以提供解决方案,我将非常感激。

2 个答案:

答案 0 :(得分:2)

最后,想通了

#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')

from random import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from random import random
from random import choice
from kivy.properties import StringProperty
import time
from kivy.clock import Clock
from functools import partial
from kivy.core.window import Window

Builder.load_string("""
<MenuScreen>:
    Button:
        text: "move to next screen 2"
        on_press: root.manager.current = 'game_mode'
<GameMode>:
    Label:
        text: "screen 2"
""")

class MenuScreen(Screen):
    pass

class GameMode(Screen):
    pass

sm = ScreenManager()
menu_screen = MenuScreen(name='menu')
sm.add_widget(menu_screen)
sm.add_widget(GameMode(name='game_mode'))

class TestApp(App):

    def build(self):
        self.bind(on_start=self.post_build_init)
        return sm

    def post_build_init(self,ev):
        from kivy.base import EventLoop
        EventLoop.window.bind(on_keyboard=self.hook_keyboard)

    def hook_keyboard(self, window, key, *largs):
        if key == 27:

            print sm.current
            if(sm.current=='menu'):
                App.get_running_app().stop()
            sm.current='menu'
            return True 



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

答案 1 :(得分:0)

ScreenManager有一个previous()方法可以解决您的问题:

Builder.load_string("""
<MenuScreen>:
    Button:
        text: "move to next screen 2"
        on_press: root.manager.current = 'game_mode'
<GameMode>:
    BoxLayout:
        orientation: "vertical"
        Button:
            text: "go back"
            on_press: root.manager.current = root.manager.previous()
        Label:
            text: "screen 2"
""")