【发布时间】: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'
【问题讨论】: