problem with changing the value of the main button in the kivymd menu

Here is your improved code

py file:

from kivy.core.window import Window
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu


Window.size = (400, 800)


class homescreen(Screen):
    pass


GUI = Builder.load_file("main.kv")

class MainApp(homescreen):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.homescreen = Builder.load_string("homescreen")
        menu_items = [{"icon": "git", "text": f"Item {i}"} for i in range(5)]
        self.menu = MDDropdownMenu(
            caller=self.ids.drop_item,
            items=menu_items,
            callback=self.set_item,
            width_mult=4,
        )


    def set_item(self, instance_menu_item):
        self.ids.drop_item.text = instance_menu_item.text
        self.menu.dismiss()

    def build(self):
        return self.homescreen

class app(MDApp):
    def build(self):
        return MainApp()

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

main.ky

<homescreen>:
    MDDropDownItem:
        id: drop_item
        pos_hint: {'center_x': .5, 'center_y': .5}
        text: "Select"
        on_release: root.menu.open()

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top