【问题标题】:How can I change default active item of Bottom Navigation in kivymd?如何更改 kivymd 中底部导航的默认活动项?
【发布时间】:2021-09-11 17:07:41
【问题描述】:

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

有什么方法可以实现吗?

【问题讨论】:

  • 您是否需要将其设为默认但始终位于底部工具栏的中心?还是您需要它作为默认值但在底部工具栏的左侧?
  • - 我需要其中任何一个作为默认值,只要我愿意。例如,在某些页面中我需要它作为默认值但在底部工具栏的左侧,在其他页面中作为默认值但始终在底部工具栏的中心。
  • 好吧,侧底图标按下时应该移动到底栏的中心对吧?,总是在中心,因为图标被点击了,可以通过切换标签文本和图标并将其移动到中心,我会尝试这样做

标签: python navigation kivy kivymd bottom


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多