Search code examples
pythonkivykivy-languagekivymd

the text inside the mddropdownmenu is not showing up... kivymd


so i have basically copied the code in the documentation but the text is not showing up and the same thing happens in my app too. no idea why

Documentation code:

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu

KV = """
#:import toast kivymd.toast.toast

MDScreen:

    MDDropDownItem:
        id: dropdown_item
        text: 'Item 0'
        pos_hint: {'center_x': .5, 'center_y': .6}
        current_item: 'Item 0'
        on_release: app.menu.open()

    MDRaisedButton:
        pos_hint: {'center_x': .5, 'center_y': .3}
        text: 'Check Item'
        on_release: toast(dropdown_item.current_item)
"""


class Example(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = Builder.load_string(KV)
        menu_items = [{"text": f"Item {i}"} for i in range(5)]
        self.menu = MDDropdownMenu(
            caller=self.screen.ids.dropdown_item,
            items=menu_items,
            position="center",
            width_mult=4,
        )
        self.menu.bind(on_release=self.set_item)

    def set_item(self, instance_menu, instance_menu_item):
        self.screen.ids.dropdown_item.set_item(instance_menu_item.text)
        instance_menu.dismiss()

    def build(self):
        return self.screen


Example().run()

the output is:

  1. the main screen ---> enter image description here
  2. after clicking the dropdownitem ---> enter image description here

Solution

  • the MDDropdownMenu use recycle view to present children and so they need to specify the viewclass or the widget type you want to display I just update your code to use OneLineListItem to be MDDropdownMenu children

    from kivy.lang import Builder
    from kivy.metrics import dp
    from kivy.properties import StringProperty
    
    from kivymd.uix.list import OneLineIconListItem
    from kivymd.app import MDApp
    from kivymd.uix.menu import MDDropdownMenu
    
    KV = '''
    <IconListItem>
    
        IconLeftWidget:
            icon: root.icon
    
    
    MDScreen
    
        MDDropDownItem:
            id: drop_item
            pos_hint: {'center_x': .5, 'center_y': .5}
            text: 'Item 0'
            on_release: app.menu.open()
    '''
    
    
    class IconListItem(OneLineIconListItem):
        icon = StringProperty()
    
    
    class Test(MDApp):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.screen = Builder.load_string(KV)
            menu_items = [
                {
                    "viewclass": "IconListItem",
                    "icon": "git",
                    "text": f"Item {i}",
                    "height": dp(56),
                    "on_release": lambda x=f"Item {i}": self.set_item(x),
                } for i in range(5)
            ]
            self.menu = MDDropdownMenu(
                caller=self.screen.ids.drop_item,
                items=menu_items,
                position="center",
                width_mult=4,
            )
            self.menu.bind()
    
        def set_item(self, text_item):
            self.screen.ids.drop_item.set_item(text_item)
            self.menu.dismiss()
    
        def build(self):
            return self.screen
    
    
    Test().run()