我是kivyMD的新手,我遇到了这个问题。我试图将TextField与下拉框绑定,当从下拉框中选择一个项目来填充TextFied中的文本时,但是当我单击一个项目时,什么都没有发生。你能帮我吗?
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()
从主分支-pip install https://github.com/kivymd/KivyMD/archive/master.zip
更新KivyMD
库
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