简体   繁体   中英

How do I switch screens with KivyMD toolbar

I made a very basic App, with KivyMD bottom App bar. I am having issues when I trying to use the toolbar icons to switch screen.

here's my main.py file (please bear with me, my code is a little clustered from me implementing some solutions I found, which didnt work)

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()

here's my .kv file

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]]

I'll appreciate any help, thanks!

Your code:

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

is trying to call the callback() method of root.manager , which is the ScreenManager . But ScreenManager does not have a callback() method. I think you want to call the callback() method of your TwoScreenApp , which you would do like this:

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

which uses the app keyword to reference the App instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM