Kivy ActionBar返回按钮切换到上一个屏幕

时间:2018-01-28 16:46:56

标签: python android-actionbar kivy

我正在尝试在Android上运行这种类型的代码但是当我点击移动后退按钮时,尽管进入上一个屏幕应用程序已关闭, 我使用这段代码来阻止App关闭:

def __init__(self, **kwargs):
    super(MyApp, self).__init__(**kwargs)
    Window.bind(on_keyboard=self.BackButton)


def BackButton(self, window, key, *args):
    if key==27:
        return True

但是如何返回上一个屏幕。以及如何使用ActionPrevious Button进入上一个屏幕?

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen

Builder.load_string("""
<MyAppGUI>:
    orientation:'vertical'
    id: box
    ActionBar:
        ActionView:
            ActionPrevious:
                title: 'How to go back using this button???'
                with_previous: True

    ScreenManager:
        id: screenmanger
        Screen:
            name: 's1'
            Button:
                text: 'goto screen 2'
                on_release: screenmanger.current='s2'
        Screen:
            name: 's2'
            Button:
                text: 'goto screen 3'
                on_release: screenmanger.current='s3'
        Screen:
            name: 's3'
            Button:
                text: 'goto main screen'
                on_release: screenmanger.current='s1'




""")

class MyAppGUI(BoxLayout):
    pass

class MyApp(App):

    def build(self):
        return MyAppGUI()


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

1 个答案:

答案 0 :(得分:1)

要使用ActionPrevious按钮返回上一个窗口,您只需将其on_release事件绑定到使用current属性的函数,即使用{返回的名称设置上一个屏幕{1}}方法或使用列表作为所访问窗口的历史记录。要在Android上使用 Back 键,您的代码原则上是正确的,至少在我的设备上它没有问题:

ScreenManager.previous ()