对于 KivyMD MDBottomNavigationItem,是否可以更改文本使其不全部大写?

For a KivyMD MDBottomNavigationItem, is it even possible to change the text so that it is not all caps?

我正在尝试创建一个底部导航栏,其中导航项的文本并非全部大写。

奇怪的是,MDBottomNavigation 默认字体或文本样式似乎全部大写。例如,底部导航栏中的主页按钮文本显示:HOME。相反,我希望它出现:Home.

下面是我在 Python 和 Kivy/KivyMD 中所做的。

Python:

from kivy.lang import Builder
from kivymd.app import MDApp

root_kv = "navigation.kv"


# Initialize Application
class TennisApp(MDApp):
    def build(self):
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "Green"
        self.root = Builder.load_file(root_kv)


# Deploy Application
TennisApp().run()

基维:

BoxLayout:
    orientation: "vertical"

    MDToolbar:
        title: "Navbar"
        md_bg_color: .2,.2,.2,1
        specific_text_color: .7,.7,.7,1

    MDBottomNavigation:
        panel_color: 0,0,0,1

        MDBottomNavigationItem:
            name: "screen1"
            text: "Home"
            icon: "home"

您可以获得“MDBottomNavigation”class,获得它后,您可以使用以下方式访问第一个“MDBottomNavigationItem”小部件树:

"B=self.M.children[0].children[0].children[0].children[0].children1.children[0].children[0]" 假设“self.M”等于“MDBottomNavigation”

稍后您可以将“BottomItem”下的“MDLabel”的“font_style”更改为小写或任何其他字体样式:

"B.font_style='Caption'"

你将能够改变它

编辑:

要更改文本样式,函数“change800”在此处执行:

from kivy.lang import Builder
from kivymd.app import MDApp
root_kv = "navigation.kv"
c="""
BoxLayout:
    orientation: "vertical"
    MDToolbar:
        title: "Navbar"
        md_bg_color: .2,.2,.2,1
        specific_text_color: .7,.7,.7,1
    MDBottomNavigation:
        id: bar100
        panel_color: 0,0,0,1
        MDBottomNavigationItem:
            id: item400
            name: "screen1"
            text: "Home"
            icon: "home"
"""
from kivy.clock import Clock
class TennisAppjbsidis(MDApp):
    def change800(self):
        M=self.root.ids.bar100
        TEXT=M.children[0].children[0].children[0].children[0].children[0]
        TEXT.font_style="Caption"
    def build(self):
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "Green"
        self.root = Builder.load_string(c) #load_file(root_kv)
        Clock.schedule_once(lambda x:self.change800(),2)
TennisAppjbsidis().run()

图片:

新编辑: 关于所有 4 个底部项目具有相同的 font_style:

from kivy.lang import Builder
from kivymd.app import MDApp
root_kv = "navigation.kv"
c="""
BoxLayout:
    orientation: "vertical"
    MDToolbar:
        title: "Navbar"
        md_bg_color: .2,.2,.2,1
        specific_text_color: .7,.7,.7,1
    MDBottomNavigation:
        id: bar100
        panel_color: 0,0,0,1
        
        MDBottomNavigationItem:
            id: item400
            name: "screen1"
            text: "Home"
            icon: "home"
            
        MDBottomNavigationItem:
            id: item500
            name: "screen2"
            text: "Edit"
            icon: "pencil"
            
        MDBottomNavigationItem:
            id: item600
            name: "screen3"
            text: "Add"
            icon: "plus"
            
        MDBottomNavigationItem:
            id: item700
            name: "screen4"
            text: "Notify"
            icon: "bell"
            
"""
from kivy.clock import Clock
class TennisAppjbsidis(MDApp):
    def change800(self):
        w="""
The following variable M is the widget:
############################
    MDBottomNavigation:
        id: bar100
        panel_color: 0,0,0,1
############################

        That's why is:
        M=self.root.ids.bar100

        So if we use:

        print(M.children)

        That equals to all the 4 MDBottomNavigationItem items of MDBottomNavigation:

        print(M.children):
        === [
        <kivymd.uix.bottomnavigation.MDBottomNavigationBar object at 0x7f15f11e0350>,
        <kivy.uix.screenmanager.ScreenManager object at 0x7f15f11e00b0>
        ]

        so that's just like we have a list like:

        === [A, B]
        where A is MDBottomNavigationBar widget in the widget tree,
        Remember:
        The original structure of the internal class MDBottomNavigationBar is:
###########################        
<MDBottomNavigation>
    id: panel
    orientation: 'vertical'
    height: dp(56)  # Spec

    ScreenManager:
        id: tab_manager
        transition: sm.FadeTransition(duration=.2)
        current: root.current
        screens: root.tabs

    MDBottomNavigationBar: #=== HERE IS THAT WIDGET TREE LOCATED, SO UNDER HIM, THERE ARE BOTTOM ITEMS
        id: bottom_panel
        size_hint_y: None
        height: dp(56)
        md_bg_color: root.theme_cls.bg_dark if not root.panel_color else root.panel_color

        BoxLayout:
            id: tab_bar
            pos_hint: {'center_x': .5, 'center_y': .5}
            height: dp(56)
            size_hint: None, None
###############################
        So having:
        TEXT=M.children[0]
        print(TEXT)
        is equal to:
        ===<kivymd.uix.bottomnavigation.MDBottomNavigationBar object at 0x7f3618dbc350>

        That means is equal to:
        ###############################
        MDBottomNavigationBar: #=== HERE IS THAT WIDGET TREE LOCATED, SO UNDER HIM, THERE ARE BOTTOM ITEMS
            id: bottom_panel
            size_hint_y: None
            height: dp(56)
            md_bg_color: root.theme_cls.bg_dark if not root.panel_color else root.panel_color

            BoxLayout:
                id: tab_bar
                pos_hint: {'center_x': .5, 'center_y': .5}
                height: dp(56)
                size_hint: None, None
                #here the bottom items are added because:

                ------------------------------------
                MDBottomNavigationItem:
                    id: item400
                    name: "screen1"
                    text: "Home"
                    icon: "home"
                    
                MDBottomNavigationItem:
                    id: item500
                    name: "screen2"
                    text: "Edit"
                    icon: "pencil"
                    
                MDBottomNavigationItem:
                    id: item600
                    name: "screen3"
                    text: "Add"
                    icon: "plus"
                    
                MDBottomNavigationItem:
                    id: item700
                    name: "screen4"
                    text: "Notify"
                    icon: "bell"

                ------------------------------------

        So now, if we use:
        
        TEXT=M.children[0].children[0]
        print(TEXT)
        is equal to:
                ==== <kivy.uix.boxlayout.BoxLayout object at 0x7fe7e8096740>

        That "BoxLayout" widget is the same like:

        ------------------------
        MDBottomNavigationBar: #=== HERE IS THAT WIDGET TREE LOCATED, SO UNDER HIM, THERE ARE BOTTOM ITEMS
            id: bottom_panel
            size_hint_y: None
            height: dp(56)
            md_bg_color: root.theme_cls.bg_dark if not root.panel_color else root.panel_color

            BoxLayout: #=== THIS IS --- TEXT=M.children[0].children[0]  ---
                id: tab_bar
                pos_hint: {'center_x': .5, 'center_y': .5}
                height: dp(56)
                size_hint: None, None
                #here the bottom items are added because:
        -----------------------------


        So now, if we use:
        
        TEXT=M.children[0].children[0].children[0]
        print(TEXT)
        is equal to:
                ==== <kivymd.uix.bottomnavigation.MDBottomNavigationHeader object at 0x7fb801b59350>

        That "MDBottomNavigationHeader" widget is the same like:
        
        ----------------------------------
        <MDBottomNavigationHeader>
            canvas:
                Color:
                    rgba: root.panel_color
                    #rgba: self.panel.theme_cls.bg_dark if not root.panel_color else root.panel_color
                Rectangle:
                    size: self.size
                    pos: self.pos

            width:
                root.panel.width / len(root.panel.ids.tab_manager.screens)\
                if len(root.panel.ids.tab_manager.screens) != 0 else root.panel.width
            padding: (dp(12), dp(12))
            on_press:
                self.tab.dispatch('on_tab_press')
            on_release: self.tab.dispatch('on_tab_release')
            on_touch_down: self.tab.dispatch('on_tab_touch_down',*args)
            on_touch_move: self.tab.dispatch('on_tab_touch_move',*args)
            on_touch_up: self.tab.dispatch('on_tab_touch_up',*args)

            FloatLayout:
                id: item_container

                MDIcon:
                    id: _label_icon
                    icon: root.tab.icon
                    size_hint_x: None
                    text_size: (None, root.height)
                    height: self.texture_size[1]
                    theme_text_color: 'Custom'
                    text_color: root._current_color
                    valign: 'middle'
                    halign: 'center'
                    opposite_colors: root.opposite_colors
                    pos: [self.pos[0], self.pos[1]]
                    font_size: dp(24)
                    pos_hint: {'center_x': .5, 'center_y': .7}

                MDLabel:
                    id: _label
                    text: root.tab.text
                    font_style: 'Button'
                    size_hint_x: None
                    text_size: (None, root.height)
                    height: self.texture_size[1]
                    theme_text_color: 'Custom'
                    text_color: root._current_color
                    valign: 'bottom'
                    halign: 'center'
                    opposite_colors: root.opposite_colors
                    font_size: root._label_font_size
                    pos_hint: {'center_x': .5, 'center_y': .6}
        ---------------------------------

        So as you can see, there are 2 more widgets under the widget tree "MDBottomNavigationHeader"
        They are:
                ------------------------------------------------------
                MDIcon: #the icon of the Bottom item
                    id: _label_icon
                    icon: root.tab.icon
                    size_hint_x: None
                    text_size: (None, root.height)
                    height: self.texture_size[1]
                    theme_text_color: 'Custom'
                    text_color: root._current_color
                    valign: 'middle'
                    halign: 'center'
                    opposite_colors: root.opposite_colors
                    pos: [self.pos[0], self.pos[1]]
                    font_size: dp(24)
                    pos_hint: {'center_x': .5, 'center_y': .7}

                MDLabel: #the text of the Bottom item
                    id: _label
                    text: root.tab.text
                    font_style: 'Button'
                    size_hint_x: None
                    text_size: (None, root.height)
                    height: self.texture_size[1]
                    theme_text_color: 'Custom'
                    text_color: root._current_color
                    valign: 'bottom'
                    halign: 'center'
                    opposite_colors: root.opposite_colors
                    font_size: root._label_font_size
                    pos_hint: {'center_x': .5, 'center_y': .6}
                ----------------------------------------
        -----------------------------


        So now, if we use:
        
        TEXT=M.children[0].children[0].children[0].children[0]
        print(TEXT)
        is equal to:
                ==== <kivy.uix.floatlayout.FloatLayout object at 0x7fbf047775f0>

        That "FloatLayout" widget is the same like:

        -----------------------------
            FloatLayout:
                id: item_container
        -----------------------------
        in the original class "MDBottomNavigationHeader",
        
        -----------------------------


        So now, if we use:
        
        TEXT=M.children[0].children[0].children[0].children[0].children[0]
        print(TEXT)
        is equal to:
                ==== <kivymd.uix.label.MDLabel object at 0x7f52867f5900>

        That "MDLabel" widget is the TEXT under the bottom item icon, that's
        why you can change the "font_style"
        using:

        TEXT.font_style="Caption"

        That comes from:

        ---------------------------------
                MDLabel: #the text of the Bottom item
                    id: _label
                    text: root.tab.text
                    font_style: 'Button' #======= HERE is the TEXT.font_style="Caption"
                    size_hint_x: None
                    text_size: (None, root.height)
                    height: self.texture_size[1]
                    theme_text_color: 'Custom'
                    text_color: root._current_color
                    valign: 'bottom'
                    halign: 'center'
                    opposite_colors: root.opposite_colors
                    font_size: root._label_font_size
                    pos_hint: {'center_x': .5, 'center_y': .6}
        --------------------------------

        
        ###############################


        
        
"""
        M=self.root.ids.bar100
        print(M.children[0].children[0].children[2].children[0].children[0])
##################################################################
        BELL_BOTTOM_ITEM="""
        ##        MDBottomNavigationItem:
        ##            id: item700
        ##            name: "screen4"
        ##            text: "Notify"
        ##            icon: "bell"
        """
        TEXT=M.children[0].children[0].children[0].children[0].children[0]
        TEXT.font_style="Caption"
##################################################################
##################################################################
        PLUS_BOTTOM_ITEM="""
        ##        MDBottomNavigationItem:
        ##            id: item600
        ##            name: "screen3"
        ##            text: "Add"
        ##            icon: "plus"
        """
        TEXT=M.children[0].children[0].children[1].children[0].children[0]
        TEXT.font_style="Caption"
##################################################################
##################################################################
        PENCIL_BOTTOM_ITEM="""                  
        ##            MDBottomNavigationItem:
        ##                id: item500
        ##                name: "screen2"
        ##                text: "Edit"
        ##                icon: "pencil"
        """
        TEXT=M.children[0].children[0].children[2].children[0].children[0]
        TEXT.font_style="Caption"
##################################################################
##################################################################
        HOME_BOTTOM_ITEM="""
        ##        MDBottomNavigationItem:
        ##            id: item400
        ##            name: "screen1"
        ##            text: "Home"
        ##            icon: "home"
        """
        TEXT=M.children[0].children[0].children[3].children[0].children[0]
        TEXT.font_style="Caption"
##################################################################

        
    def build(self):
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "Green"
        self.root = Builder.load_string(c) #load_file(root_kv)
        Clock.schedule_once(lambda x:self.change800(),2)
TennisAppjbsidis().run()

新编辑的新图片: