来自KivyMD的MDDropDownMenu错误(对于Python 3.6上的Kivy)

时间:2019-08-10 08:32:16

标签: python-3.x kivy

单击下拉菜单标签时出现问题。 它显示了一个错误:AttributeError:'MDMenuItem'对象没有属性'callback'

这是我的main.py

from kivy.app import App
from kivymd.theming import ThemeManager
from kivy.uix.screenmanager import Screen


class MyScreen(Screen):
    VARIABLE = ""

    menu_items = [
    {'viewclass': 'MDMenuItem',
     'text': 'text1'},
    {'viewclass': 'MDMenuItem',
     'text': 'text2'},
    ]

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


class MainApp(App):
    title = "KivyMD MDDropdownMenu Demo"
    theme_cls = ThemeManager()

    def build(self):
        return MyScreen()


if __name__ == "__main__":
    MainApp().run()

这是一个main.kv

#:import MDDropdownMenu kivymd.menus.MDDropdownMenu
#:import MDRaisedButton kivymd.button.MDRaisedButton

<MDMenuItem>:
    on_release: app.root.change_variable(self.text)

<MyScreen>:
    name: 'myscrn'
    MDRaisedButton:
        size_hint: None, None
        size: 3 * dp(48), dp(48)
        text: 'MDButton'
        opposite_colors: True
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}
        on_release: MDDropdownMenu(items=root.menu_items, width_mult=4).open(self)

1 个答案:

答案 0 :(得分:1)

对于KivyMD version 0.100.2,您必须提供一个callback函数。

摘要

def toast(text):
    from kivymd.toast.kivytoast import toast

    toast(text)


class MyScreen(Screen):
    VARIABLE = ""

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.menu_items = [
            {
                "viewclass": "MDMenuItem",
                "text": "text%d" % i,
                "callback": self.callback_for_menu_items,
            }
            for i in range(1, 3)
        ]

    def callback_for_menu_items(self, *args):
        toast(args[0])

输出

Menu drop down Menu selected