Kivy: Error while trying to capture options from MDDropDownMenu

74 views Asked by At

I am new to kivy and am trying to add a DropDown list and trying to capture the options being clicked in a variable, but I seem to run into many errors while doing so. This is the error that I am getting: enter image description here

This is my main.py file:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty
from kivy.vector import Vector
from kivy.uix.label import Label
from kivy.uix.button import Button  
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.behaviors import RectangularElevationBehavior
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.config import Config
from kivymd.app import MDApp
from kivy.lang import Builder

Config.set('graphics', 'resizable', False)
class CustomToolbar(ThemableBehavior, RectangularElevationBehavior, MDBoxLayout,):
   def __init__(self, **kwargs):
       super().__init__(**kwargs)
       self.md_bg_color = self.theme_cls.primary_color

class MainApp(MDApp):
   def __init__(self, **kwargs):
       self.VARIABLE = ""
       super().__init__(**kwargs)
       self.screen = Builder.load_file("main.kv")

       menu_items = [{"viewclass": "MDMenuItem","text": "Label1"},{"viewclass":
       "MDMenuItem","text": "Label2"}]
       self.menu = MDDropdownMenu(
       caller=self.screen.ids.drop_item,
       items=self.menu_items,
       position="center",
       width_mult=3,
        )
       self.menu.bind(on_release=self.menu_callback)

   def change_variable(self, value):
       print("\nvalue=", value)
       self.VARIABLE = value
       print("\tself.VARIABLE=", self.VARIABLE)

   def menu_callback(self, instance_menu, instance_menu_item):
       instance_menu.dismiss()

   def build(self):
       return self.screen
    
 MainApp().run()

This is my .kv file:

<ScreenManagement>:
id: scr_mngr
MainAppScreen:
    name: 'main'
    id: main
<CustomToolbar>:
#some widgets here
 BoxLayout:
    #some widgets here
<MDMenuItem>:
on_release: app.root.change_variable(self.text)
Screen:
    MDRaisedButton:
        id: drop_item
        text: "W"
        pos_hint: {"center_x": .9, "center_y": .68}
        on_release: app.menu.open()

I am stuck at this for 2 days, please help. Thanks in advance :D

1

There are 1 answers

2
John Anderson On

I think your code will work if you build the GUI in the build() method and use Clock.schedule_once() to setup the menu, like this:

class MainApp(MDApp):
   def change_variable(self, value):
       print("\nvalue=", value)
       self.VARIABLE = value
       print("\tself.VARIABLE=", self.VARIABLE)

   def menu_callback(self, instance_menu, instance_menu_item):
       instance_menu.dismiss()

   def build(self):
       self.VARIABLE = ""
       self.screen = Builder.load_file("main.kv")

       Clock.schedule_once(self.setup_menu)
       return self.screen

   def setup_menu(self, dt):
       menu_items = [{"viewclass": "MDMenuItem","text": "Label1"},{"viewclass":
       "MDMenuItem","text": "Label2"}]
       self.menu = MDDropdownMenu(
           caller=self.screen.ids.drop_item,
           items=menu_items,
           position="center",
           width_mult=3
        )
       self.menu.bind(on_release=self.menu_callback)