我正在尝试使用kv文件添加一个下拉列表。我已经成功创建了它,但是当我单击它时,下拉菜单立即打开并关闭,而无需我选择或单击任何地方
我该如何停止
Button:
id: btn
text: 'Press'
on_release:
if not root.state: dropdown.open(self); root.state = False
else: dropdown.dismiss(); root.state = False
size_hint: 0.2,0.05
height: '48dp'
DropDown:
id: dropdown
on_parent: self.dismiss()
on_select: btn.text = '{}'.format(args[1]); root.state = False
Button:
text: 'First Item'
size_hint_y: None
height: '48dp'
on_release: dropdown.select('First Item')
Label:
text: 'Second Item'
size_hint_y: None
height: '48dp'
Button:
text: 'Third Item'
size_hint_y: None
height: '48dp'
on_release: dropdown.select('Third Item')
答案 0 :(得分:0)
如果您正在谈论MDDropDownMenu-它正在以另一种方式工作,并且接受按钮的字典列表。这是示例:
在kv文件中:
MDRaisedButton:
text: app.groupstrace
size_hint_x: 0.15
# here you call drop down menu:
on_release: MDDropdownMenu(items=app.groupsdict, width_mult=3, background_color=[1,0.5,0,1]).open(self)
md_bg_color: [1,0.5,0,1]
在.py文件中:
groupsdict = [
{
"viewclass": "MDMenuItem",
"text": "Yourtext",
# no parentheses after function name!
"callback": self.function1,
},
{
"viewclass": "MDMenuItem",
"text": "Yourtext",
# in case you want label, just don't set a callback
"callback": None,
}
# and so on
]