2

I am new with kivyMD and I get stuck with this problem. I am trying to bind TextField with DropDown and when select an item from DropDown to fill the text in the TextFied, but when I click on an item nothing happen. Can you help me?

from kivy.clock import Clock
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu

KV = '''
Screen
    MDTextField:
        id: field
        pos_hint: {'center_x': .5, 'center_y': .5}
        size_hint_x: None
        width: "200dp"
        hint_text: "Material"
        on_focus: if self.focus: app.menu.open()
'''


class Test(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = Builder.load_string(KV)

        items = ['PVC', 'XLPE']

        menu_items = [{"text": f"{i}"} for i in items]

        self.menu = MDDropdownMenu(
                                    caller=self.screen.ids.field,
                                    items=menu_items,
                                    position="bottom",
                                    width_mult=3,
                                    )

        self.menu.bind(on_release=self.set_item)

    def set_item(self, instance_menu, instance_menu_item):
        def set_item(interval):
            self.screen.ids.field.text = instance_menu_item.text
            instance_menu.dismiss()
        Clock.schedule_once(set_item, 0.5)

    def build(self):
        return self.screen


Test().run()
Andonov85
  • 77
  • 8

2 Answers2

0

Update KivyMD library from master branch - pip install https://github.com/kivymd/KivyMD/archive/master.zip

Xyanight
  • 1,315
  • 1
  • 7
  • 10
0
class Test(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = Builder.load_string(KV)
        menu_items = [
            {
                "text": f"Item {i}",
                "viewclass": "OneLineListItem",
                "on_release": lambda x=f"Item {i}": self.menu_callback(x),
            } for i in range(5)
        ]
        self.menu = MDDropdownMenu(
            caller=self.screen.ids.button,
            items=menu_items,
            width_mult=4,
        )

    def menu_callback(self, text_item):
        print(text_item)

    def build(self):
        return self.screen
halfelf
  • 9,737
  • 13
  • 54
  • 63