3

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()
zupp
  • 96
  • 7

2 Answers2

0

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() 
Hussam F. Alkdary
  • 655
  • 1
  • 6
  • 21
0

I fixed it by adding viewless in each drop down item while defining menu_items. Fixed sample is as follows:

menu_items = [{'viewclass': 'MDDropDownItem', "icon": "git", "text": f"Item {i}"} for i in range(5)]
self.menu = MDDropdownMenu(
            caller=self.root.ids.button,
            items=menu_items
        )
        self.menu.open()

PinkSs
  • 1
  • 1