0

我是kivymd的初学者。我试图在 kivymd 中创建底部导航。运行代码后,它显示底部导航的活动项目是'Home'(name ='screen_1)。现在我想要第二个项目作为默认活动项目-> 'Features'(name='screen_2')。这是示例代码。

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window

Window.size=(350,593)

KV_string = """ 
Screen:
    BoxLayout:
        orientation:'vertical'
        MDToolbar:
            title:'Demo Application'
            right_action_items : [["dots-vertical"]]
            left_action_items : [["menu", lambda x: app.menu_toggle()]]
            right_action_items : [["dots-vertical", lambda x: app.option_fun()]]
            elevation:10
            
        MDLabel:
            text:'Welcome to good GUI'
            halign:'center'

        MDBottomNavigation: 
            MDBottomNavigationItem:
                name: 'screen_1'
                text: 'Home'
                icon: 'home-outline'
                on_tab_press: app.Bottom_nav_fun()

                MDLabel:
                    text: 'Home page'
                    halign: 'center'

            MDBottomNavigationItem:
                name: 'screen_2'
                text: 'Features'
                icon: 'feature-search-outline'

                MDLabel:
                    text: 'Features of this app'
                    halign: 'center'

            MDBottomNavigationItem:
                name: 'screen_3'
                text: 'Developers'
                icon: 'account-supervisor'

                MDLabel:
                    text: 'Developers details'
                    halign: 'center'
"""


class MyApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette="Blue"
        self.theme_cls.theme_style="Light"
        self.theme_cls.primary_hue="A700"
        self.screen = Builder.load_string(KV_string)
        return self.screen
    def menu_toggle(self):
        print("Menu toggle Working")
    def option_fun(self):
        print("Option method Working")
    def Bottom_nav_fun(self):
        print("Bottom nav home")

if __name__=='__main__':
    MyApp().run()

有什么方法可以实现吗?

4

1 回答 1

0

使用 switch_tab 方法,它将您要切换到的选项卡的名称作为参数 - https://kivymd.readthedocs.io/en/latest/components/bottomnavigation/#how-to-automatically-switch-a-tab

于 2021-09-12T07:02:06.503 回答