簡體   English   中英

如何使用 KivyMD 工具欄切換屏幕

[英]How do I switch screens with KivyMD toolbar

我做了一個非常基本的應用程序,帶有 KivyMD 底部應用程序欄。 我在嘗試使用工具欄圖標切換屏幕時遇到問題。

這是我的 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()方法,你TwoScreenApp

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

它使用app關鍵字來引用App實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM