使用 Kivy 更改 ActionBar 上的屏幕转换

Changing screen transition on ActionBar with Kivy

我正在使用 ScreenManager 和 Transitions。在我的代码中,我还集成了一个 ActionBar。 我试过这个:

ActionButton:
       text: 'Back to Main Menu'
       on_release: 
           app.root.ids.sm.current = 'first'
           root.manager.transition.direction = "right"

但是我得到这个错误

AttributeError: 'SomeMenu_ActionBar' object has no attribute 'manager'

我还尝试将 SomeMenu_ActionBar 与其他 Screens 放在 ScreenManager 下。也不起作用,因为 ScreenManager 只接受屏幕小部件。

是否可以更改 ActionBar 的转换?最好我根本不想要任何过渡,对于所有按钮。我是否可能必须在每个看起来像 ActionBar 的屏幕中添加一个 BoxLayout,这可能可行?

这是我的 pythoncode 和 kv:

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.metrics import dp, sp
from kivy.lang import Builder

class WelcomeScreen(Screen):
    pass

class FirstScreen(Screen):
    pass

class SecondScreen(Screen):
    pass

class ScreenManager(ScreenManager):
    pass

class CrimePrevention(BoxLayout):
    pass

Builder.load_file("screenmanager.kv")

class TestApp(App):
    title = "Kivy ScreenManager & ActionBar Demo"

    def build(self):
        return CrimePrevention()
    
if __name__ == '__main__':
    TestApp().run()

kv-文件:

#:kivy 2.1.0

<CrimePrevention>:
    orientation: "vertical"

    canvas.before:
        Color:
            rgb: .6, .6, .6
        Rectangle:
            pos: self.pos
            size: self.size
    
    SomeMenu_ActionBar:
        id: ActionBar

    ScreenManager:
        id: sm
        WelcomeScreen:
        FirstScreen:
        SecondScreen:

<SomeMenu_ActionBar@ActionBar>:

    ActionView:
        id: ActionView

        HiddenIcon_ActionPrevious:

        ActionGroup:
            id: App_ActionGroup
            mode: "spinner"
            text: "Jump to Screen"

            ActionButton:
                text: "Crime Prediction"
                on_release: app.root.ids.sm.current = 'second'
            ActionButton:
                text: "Forum"
                on_release: app.root.ids.sm.current = 'second'
            ActionButton:
                text: "Probable Suspect"
                on_release: app.root.ids.sm.current = 'second'
        
        ActionGroup:
            id: App_ActionGroup
            mode: 'spinner'
            text: 'App'

            ActionButton:
                text: 'Settings'
                on_press: app.open_settings()
            ActionButton:
                text: 'Quit'
                on_press: app.get_running_app().stop()
        
        ActionGroup:
            id: File_ActionGroup
            mode: 'spinner'
            text: 'File'

            ActionButton:
                text: 'Open'
            ActionButton:
                text: 'Save'
        
        ActionButton:
            text: 'Back to Main Menu'
            on_release: 
                app.root.ids.sm.current = 'first'
                #root.manager.transition.direction = "right" 

<HiddenIcon_ActionPrevious@ActionPrevious>:
    title: ''
    with_previous: False
    app_icon: ''
    app_icon_width: 0
    app_icon_height: 0
    size_hint_x: None
    width: len(self.title)*10

<WelcomeScreen>:
    name: 'welcome'
    Label:
        text: 'Welcome Screen'
        font_size: sp(50)

<FirstScreen>:
    name: 'first'
    Label:
        text: 'First Screen'

<SecondScreen>:
    name: 'second'
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Predict Crime'
            font_size: 50
        
        BoxLayout:
            Button:
                text: 'Back to Main Menu'
                font_size: 30
                on_release: 
                    app.root.ids.sm.current = 'first'
                    #root.manager.transition.direction = "right"
            Button:
                text: 'get random colour screen'
                font_size: 30
                on_release: 
                    app.root.ids.sm.current = 'first'
                    #root.manager.transition.direction = "right"

问题是您的代码试图访问 SomeMenu_ActionBarmanager 属性。发生这种情况是因为 root(如 documentation 中所述)是它出现的规则的根小部件。所以 root 在这种情况下是 SomeMenu_ActionBar。在 kivy 文档中过度使用术语 root 可能会导致一些混淆。还有一个root widget 可以在kv 文件中定义,但是你的kv 文件没有定义这样的root。还有 kivy Applicationroot 小部件。所以在你的代码中:

    ActionButton:
        text: 'Back to Main Menu'
        on_release: 
            app.root.ids.sm.current = 'first'
            root.manager.transition.direction = "right" 

root指的是包含SomeMenu_ActionBar。您可以修改该代码(如下所示)以访问 App:

root 小部件
    ActionButton:
        text: 'Back to Main Menu'
        on_release: 
            app.root.ids.sm.current = 'first'
            app.root.ids.sm.transition.direction = "right"