MDDropdownMenu 调用者的 KivyMD KeyError

KivyMD KeyError on caller for MDDropdownMenu

我正在尝试更改我现有的 Kivy 应用程序以合并漂亮的 KivyMD 小部件。我删除了 MDRaisedButtonSpinneron_release callerMDDropdownMenu 以及已通过蓝牙与设备配对的设备名称列表(取决于在平台上)。对于此测试示例,我仅使用 MDDropdownMenu 文档示例中所示的菜单项列表。

出于某种原因,我在 MDRaisedButton id 初始化 MDDropdownMenu 属性时收到 KeyError: 'dev_button'__init__ 方法在我的根目录 class 中,它在名为 indexui.pyindexui.kv 的单独文件中到 main.pymain.kv 文件。但是在 main.py 文件中导入并包含在 main.kv 文件中。该应用在更改 Spinner 之前执行了 运行。

indexui.py

class RootLay(FloatLayout):
    this = ObjectProperty(None)  # App object
    bserial = ObjectProperty(None)  # Bluetooth variables starts
    recv_stream = ObjectProperty(None)
    send_stream = ObjectProperty(None)
    menu = ObjectProperty(None)
    rfsocket = ObjectProperty(None)
    device = ObjectProperty(None)
    pattern = ObjectProperty(None)
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.device = None
        self.pattern = re.compile(r'[GNSTUkg,\s]')
        self.dev_list = []
        menu_items = [{"icon": "git", "text": f"Item {i}"} for i in range(5)]
        print(dir(self.ids))
        self.menu = MDDropdownMenu(
            caller=self.ids.dev_button, items=menu_items, width_mult=4
        )
        self.menu.bind(on_release=self.menu_callback)

indexui.kv

<RootLay>:
    MDToolbar:
        pos_hint: {'top': 1, 'right': 1}
        title: "Bluetooth Weigh"
    MDRaisedButton:
        id: dev_button
        text: "SEE DEVICES"
        pos_hint: {'top': 0.76, 'right': 0.9}
        on_release: app.root.menu.open()

main.py

from kivymd.app import MDApp
from views.indexui import RootLay
from kivy.properties import ObjectProperty

class MainApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Yellow"  # "Purple", "Red"
        self.theme_cls.primary_hue = "600"
        # self.icon = 'bluetooth-connected_326502.png'
        root = ObjectProperty(RootLay())
        return root

    def on_pause(self):
        return True
    
MainApp().run()

main.kv

#:include views/indexui.kv

RootLay:

有人可以帮我理解为什么会有 KeyError 吗? MDRaisedButton 作为方法在 RootLay class 范围内。因此,我应该能够访问它的所有属性。

我认为问题在于您试图在 RootLay class 的 ids 可用之前访问它们。我建议像这样使用 Clock.schedule_once() 来延迟访问:

class RootLay(FloatLayout):
    this = ObjectProperty(None)  # App object
    bserial = ObjectProperty(None)  # Bluetooth variables starts
    recv_stream = ObjectProperty(None)
    send_stream = ObjectProperty(None)
    menu = ObjectProperty(None)
    rfsocket = ObjectProperty(None)
    device = ObjectProperty(None)
    pattern = ObjectProperty(None)
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.device = None
        self.pattern = re.compile(r'[GNSTUkg,\s]')
        self.dev_list = []
        Clock.schedule_once(self.setup_menu)  # delay until ids are available

    def setup_menu(self, dt):
        menu_items = [{"icon": "git", "text": f"Item {i}"} for i in range(5)]
        self.menu = MDDropdownMenu(
            caller=self.ids.dev_button,
            items=menu_items,
            width_mult=4
        )
        self.menu.bind(on_release=self.menu_callback)