how can i fix this dropdown menu in kivymd?

Viewed 196

I'm new on kivy/kivymd and I am trying to create a dropdown menu through the kivymd documentation. However, whenever I click on the dropdown it returns this error to me:

AttributeError: 'Box_2' object has no attribute 'menu'

I'm not sure what have i done wrong, so any help is welcome.

this is my .py file: (as it appears in the documentation)

class Box_2(GridLayout):
    def set_item(self, instance_menu, instance_menu_item):
        menu_items = [{"icon": "git", "text": f"Item {i}"} for i in range(5)]
        self.menu = MDDropdownMenu(
            caller=self.Box_2.ids.drop_item,
            items=menu_items,
            position="center",
            width_mult=4,
        )
        self.menu.bind(on_release=self.set_item)

        self.Box_2.ids.drop_item.set_item(instance_menu_item.text)
        self.menu.dismiss() 

and this is my .kv file:

<Box_2>:
    MDDropDownItem:
        id: drop_item
        text: 'Projeto'
        on_release: root.menu.open()
1 Answers

you should initiate the menu variable in Box_2 class so now Box_2 has variable and also the set set_item should be called when you create the class using init method

class Box_2(GridLayout):
    menu =None #add this line 
    def set_item(self, instance_menu, instance_menu_item):
        menu_items = [{"icon": "git", "text": f"Item {i}"} for i in range(5)]
        self.menu = MDDropdownMenu(
            caller=self.Box_2.ids.drop_item,
            items=menu_items,
            position="center",
            width_mult=4,
        )
        self.menu.bind(on_release=self.set_item)

        self.Box_2.ids.drop_item.set_item(instance_menu_item.text)
        self.menu.dismiss() 
Related