【问题标题】:kivymd MDbottomnavigation on_tab_presskivymd MDbottomnavigation on_tab_press
【发布时间】:2021-05-11 19:16:22
【问题描述】:

这是我第一次尝试 KivyMD。我有以下 kivymd 底部导航选项卡。 我正在尝试获取按下的选项卡的名称,以便我想在其中一个选项卡中显示数据表等,在另一个选项卡中显示一些配置文件功能等。 但是,当我尝试在MDBottomNavigation 中使用on_tab_press 事件时,它可以工作。但是,它给了我一个对象而不是实际值(按下的选项卡名称)。 在下面的main.py 中,on_tab_press 函数给了我以下信息。如何从on_tab_press 事件中获取名称?

print(type(args[0])) - <class 'kivymd.uix.bottomnavigation.MDBottomNavigationItem'>
print(*args) - <MDTab name='screen1', text='tab1'>

main.py

from kivymd.theming import ThemeManager

class MainApp(MDApp):

    def build(self):
        theme_cls = ThemeManager()
        self.root = Builder.load_file("main.kv")
        return self.root

    def on_tab_press(self, *args):
        print(type(args[0]))
        print(*args)
        # returns 
        # <class 'kivymd.uix.bottomnavigation.MDBottomNavigationItem'>
        # <MDTab name='screen1', text='tab1'>

main.kv

<BottomNav>:
    #name: 'hello'
    BoxLayout:
        orientation:'vertical'
        MDToolbar:
            title: 'folio'
            md_bg_color: .2, .2, .2, 1
            specific_text_color: 1, 1, 1, 1
        MDBottomNavigation:
            panel_color: 1,1,1,1
            MDBottomNavigationItem:
                name: 'screen1'
                text: 'tab1'
                #icon: 'language-python'
                on_tab_press: app.on_tab_press(*args)
                #on_tab_press: app.current = 'home_screen'
                #MDLabel:
                #    text: 'tab1'
                #    halign: 'center'
            MDBottomNavigationItem:
                name: 'screen2'
                text: 'tab2'
                on_tab_press: app.on_tab_press(*args)
                #icon: 'language-cpp'
                #MDLabel:
                #    text: 'I programming of C++'
                #    halign: 'center'
            MDBottomNavigationItem:
                name: 'screen3'
                text: 'tab3'
                on_tab_press: app.on_tab_press(*args)
                #icon: 'language-javascript'
                MDLabel:
                    text: 'JS'
                    halign: 'center'

【问题讨论】:

    标签: python kivymd


    【解决方案1】:

    问题

    您在 def on_tab_press 方法中使用 *args 作为参数。据我所知* 解包传递的数据并返回一个元组,这里的数据是 args 这是你的BottomNavigationItem

    但是,您传递了一个对象 kivymd.uix.bottomnavigation.MDBottomNavigationItem 并试图通过使用 print(*args) 将其视为可迭代对象,这会造成一些麻烦

    解决方案

    将您的方法更改为类似def on_press_method(self, args) 并使用print(args.name)print(args.text) 访问对象的名称文本

    示例代码

    from kivy.lang import Builder
    
    from kivymd.uix.screen import Screen
    from kivymd.app import MDApp
    from kivymd.theming import ThemeManager
    from kivymd.uix.button import MDRectangleFlatButton
    
    Builder.load_string("""
    <MainScreen>:
        BoxLayout:
            orientation:'vertical'
            MDToolbar:
                title: 'folio'
                md_bg_color: .2, .2, .2, 1
                specific_text_color: 1, 1, 1, 1
            MDBottomNavigation:
                panel_color: 1,1,1,1
                MDBottomNavigationItem:
                    name: 'screen1'
                    text: 'tab1'
                    icon: 'language-python'
                    on_tab_press: app.on_tab_press(*args)
                    #on_tab_press: app.current = 'home_screen'
                    #MDLabel:
                    #    text: 'tab1'
                    #    halign: 'center'
                MDBottomNavigationItem:
                    name: 'screen2'
                    text: 'tab2'
                    on_tab_press: app.on_tab_press(*args)
                    #icon: 'language-cpp'
                    #MDLabel:
                    #    text: 'I programming of C++'
                    #    halign: 'center'
                MDBottomNavigationItem:
                    name: 'screen3'
                    text: 'tab3'
                    on_tab_press: app.on_tab_press(*args)
                    #icon: 'language-javascript'
                    MDLabel:
                        text: 'JS'
                        halign: 'center'
    
    """)
    
    class MainScreen(Screen):
        pass
    
    class MainApp(MDApp):
        def on_tab_press(self, args):
            
            print(type(args))
            print(args.name)
            print(args.text)
    
    
        def build(self):
            return MainScreen()
    MainApp().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      相关资源
      最近更新 更多