如何使用 KivyMD 工具栏切换屏幕

How do I switch screens with KivyMD toolbar

我做了一个很基础的App,KivyMD底部的App栏。我在尝试使用工具栏图标切换屏幕时遇到问题。

这是我的 main.py 文件(请耐心等待,我的代码有点像我实现的一些我发现的解决方案,但没有用)

from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window

Window.size = (300, 500)


class Screen1(Screen):
    pass


class Screen2(Screen):
    pass


class TwoScreenApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = 'Gray'
        sm = ScreenManager()
        sm.add_widget(Screen1(name='screen1'))
        sm.add_widget(Screen2(name='screen2'))

        return sm

    def callback(self, screen):
        self.current = screen
        # root.manager.transition.direction = 'left'
        # root.manager.current = 'screen2'
        # link icon to screen2



TwoScreenApp().run()

这是我的 .kv 文件

ScreenManager:
    Screen1:
    Screen2:

<Screen1>
    name: 'screen1'
    MDBoxLayout:
    md_bg_color: (255/255, 255/255, 255/255, 1)
    orientation: 'vertical'

    MDLabel:
        halign: 'center'
        text:
            """With the production of the Model T automobile,
            Henry Ford had an unforeseen and tremendous
            impact on American life. He became regarded
            as an apt symbol of the transition from an
            agricultural to an industrial America."""

    MDBottomAppBar:

        MDToolbar:
            icon: "account-circle"
            type: "bottom"
            left_action_items: [["arrow-right", lambda x: root.manager.callback("screen1")]]
            right_action_items: [["arrow-left", lambda x: x]]
            elevation: 10

<Screen2>
    name: 'screen2'
    md_bg_color: (200/255, 200/255, 200/255, 1)

    MDBoxLayout:
        md_bg_color: (255/255, 255/255, 255/255, 1)
        orientation: 'vertical'

    MDLabel:
        halign: 'center'
        text:
            """The development of mass-production techniques, which
            enabled the company eventually to turn out a Model T
            every 24 seconds; the frequent reductions in the price
            of the car made possible by economies of scale; and
            the payment of a living wage that raised workers
            above subsistence and made them potential customers
            for, among other things, automobiles—these innovations
            changed the very structure of society."""

    MDBottomAppBar:

        MDToolbar:
            title: "Title"
            icon: "account-circle"
            type: "bottom"
            left_action_items: [["menu", lambda x: x]]

如果有任何帮助,我将不胜感激!

您的代码:

left_action_items: [["arrow-right", lambda x: root.manager.callback("screen1")]]

正在尝试调用 root.managercallback() 方法,即 ScreenManager。但是 ScreenManager 没有 callback() 方法。我认为您想调用 TwoScreenAppcallback() 方法,您可以这样做:

left_action_items: [["arrow-right", lambda x: app.callback("screen2")]]

它使用 app 关键字来引用 App 实例。