You need to enable JavaScript to run this app. 如何通过KivyMD底部工具栏MDToolbar实现Screen页面切换
优惠活动
大模型
产品
解决方案
定价
更多

如何通过KivyMD底部工具栏MDToolbar实现Screen页面切换

核心问题排查

  • ScreenManager重复创建冲突
    你在build()方法里手动创建了一个ScreenManager实例,同时kv文件顶层也定义了ScreenManager,两者冲突,运行时实际使用的实例和预期不符。
  • callback方法定位与逻辑错误
    callback方法定义在App类中,你原来的kv代码里调用root.manager.callback()是错误的,ScreenManager实例没有这个方法;同时方法内self.current = screen写法错误,current是ScreenManager的属性,不是App的属性。
  • 跳转目标错误
    Screen1里的左箭头按钮触发跳转时传参是"screen1",跳转到当前页面,自然看不到变化。
  • kv布局缩进错误
    Screen内的MDLabel、MDBottomAppBar等控件没有正确缩进归属到MDBoxLayout下,会导致布局异常。

修正后代码

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'
        # 读取kv中定义的ScreenManager并存为App属性,方便后续调用
        self.sm = self.root
        return self.sm

    def callback(self, screen):
        # 可自定义转场方向,不需要可以删掉这行
        self.sm.transition.direction = 'left'
        self.sm.current = screen
        

if __name__ == "__main__":
    TwoScreenApp().run()

twoscreen.kv(注意kv文件名要和App类名对应,全小写即可)

<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"
            # 调用App中的callback方法跳转到screen2
            left_action_items: [["arrow-right", lambda x: app.callback("screen2")]]
            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: [["arrow-left", lambda x: app.callback("screen1")]]

# 顶层定义ScreenManager
ScreenManager:
    Screen1:
    Screen2:

内容的提问来源于stack exchange,提问作者Edwin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 15:51:03