from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.menu import MDDropdownMenu
class MainWindow(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.menu = MDDropdownMenu(
caller=self.drop,
callback=self.drop_down,
width_mult=4,
items=self.menu_items
)
@staticmethod
def drop_down(instance):
print(instance.text)
class WindowManager(ScreenManager):
pass
class MyApp(MDApp):
def build(self):
return WindowManager()
if __name__ == '__main__':
MyApp().run()
my.kv
<WindowManager>:
MainWindow:
<MainWindow>:
drop:drop
Button:
id: drop
pos_hint : {'center_x': .5 , 'center_y':.5}
on_release: root.menu.open()
我试图将按钮与在 MainWindow 类中实例化的 DropDown 绑定,它工作正常,而我没有使用屏幕管理器作为根,但现在它不能正常运行。这是我运行程序时出现的错误。
AttributeError:“MainWindow”对象没有属性“drop”
提前致谢!