我想使用 DropDown 类获得一个简单的组合框,如小部件。我可以使用 python 代码来完成,但是否可以仅使用 kv 语言?
我尝试了以下方法。这是我的 Python 代码:
class CustomDropDown(DropDown):
pass
class MainForm(BoxLayout):
pass
class MainApp(App):
def build(self):
self.dropdown = CustomDropDown()
self.mainForm = MainForm()
return self.mainForm
def do_something(self):
self.dropdown.open(self.mainForm)
MainApp().run()
这是 kv 文件:
<MainForm>:
Button:
text: 'Press'
size_hint: [None,None]
height: '40dp'
on_release: app.do_something()
<CustomDropDown>:
Button:
text: 'First Item'
Label:
text: 'Disabled item'
Button:
text: 'Second Item'
但这行不通。你能建议点什么吗?感谢您的帮助。
请您参考如下方法:
是的,可以使用 kivy 语言。
您可以阅读有关 DropDownList 的信息或 Spinner通过这些链接。而且,如果您想了解更多有关他们工作的信息,您可能需要查看此 link for kivy-showcase
我认为代码是不言自明的。(on_select 方法)
这是main.py文件
from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.boxlayout import BoxLayout
class CustomDropDown(BoxLayout):
pass
class MainApp(App):
def build(self):
return CustomDropDown()
if __name__=='__main__':
MainApp().run()
这是main.kv文件
<CustomDropDown>:
Button:
id: btn
text: 'Press'
on_release: dropdown.open(self)
size_hint_y: None
height: '48dp'
DropDown:
id: dropdown
on_parent: self.dismiss()
on_select: btn.text = '{}'.format(args[1])
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')
