我有2个文件,其中一个 py 和另一个 kv ,正如标题所述,我不知道如何添加下拉列表。
---> main.py
from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.gridlayout import GridLayout
class MyGridLayout(GridLayout):
pass
class LayoutsApp(App):
def build(self):
return MyGridLayout()
if __name__ == '__main__':
LayoutsApp().run()
---> layouts.kv
<MyGridLayout>:
rows: 2
id: main_win
Dropdown:
id: dropdown
Button:
id: btn1
text: 'option 1'
Button:
id: btn2
text: 'option 2'
BoxLayout:
BoxLayout:
编译时,它将为该部分生成错误。致电下拉列表的正确方法是什么?
答案 0 :(得分:0)
调用Button id:btn2,我该怎么办? -我的意思是:上课 MyGridLayout(GridLayout):
from kivy.uix.dropdown import DropDown
; from kivy.properties import ObjectProperty
class CustomDropDown(DropDown):
dropdown = ObjectProperty(None)
;实现方法dropdown_open(self):
;实例化CustomDropDown类并将其分配给self.dropdown
,依此类推。请参见代码段详细信息。<CustomDropdown@DropDown>:
替换动态类<CustomDropDown>:
rows: 2
替换为rows: 3
Factory.CustomDropdown().open(self)
替换为root.dropdown_open(self)
from kivy.uix.dropdown import DropDown
from kivy.properties import ObjectProperty
class CustomDropDown(DropDown):
pass
class MyGridLayout(GridLayout):
dropdown = ObjectProperty(None)
def dropdown_open(self, instance):
self.dropdown = CustomDropDown()
self.dropdown.open(instance)
print(self.dropdown.ids.btn2.text)
<CustomDropDown>:
id: dropdown
on_select:
app.root.ids.btn.text = '{}'.format(args[1])
...
<MyGridLayout>:
rows: 3
id: main_win
Button:
id: btn
text: 'Press'
size_hint_y: None
height: '48dp'
on_release:
root.dropdown_open(self)
当我添加这些命令时,它不起作用:BoxLayout:方向: 'vertical'size_hint:(.9,.9)BoxLayout:方向:'vertical' size_hint_y:0.5
您可能需要将行从2增加到3。
#:kivy 1.10.1
#:import Factory kivy.factory.Factory
<CustomDropdown@DropDown>:
id: dropdown
on_select:
app.root.ids.btn.text = '{}'.format(args[1])
self.dismiss()
Button:
id: btn1
text: 'option 1'
size_hint_y: None
height: '48dp'
on_release:
dropdown.select(btn1.text)
Button:
id: btn2
text: 'option 2'
size_hint_y: None
height: '48dp'
on_release:
dropdown.select(btn2.text)
<MyGridLayout>:
rows: 2
id: main_win
Button:
id: btn
text: 'Press'
on_release: Factory.CustomDropdown().open(self)
size_hint_y: None
height: '48dp'
BoxLayout:
orientation: 'vertical'
size_hint: (.9,.9)
canvas.before:
Color:
rgba: 1, 0, 0, 1 # red
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
size_hint_y: 0.5
canvas.before:
Color:
rgba: 0, 0, 1, 1 # blue
Rectangle:
pos: self.pos
size: self.size
DropDown小部件类似于Popup小部件,即它们是特殊的小部件。
Popup / DropDown是一个特殊的小部件。请勿尝试将其作为其他孩子添加 小部件。如果您这样做,将像普通小部件一样处理Popup / DropDown 不会隐藏在后台。
因此,创建一个具有DropDown小部件继承的动态类,并使用Factory实例化该类。
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
class MyGridLayout(GridLayout):
pass
class LayoutsApp(App):
def build(self):
return MyGridLayout()
if __name__ == '__main__':
LayoutsApp().run()
#:kivy 1.10.1
#:import Factory kivy.factory.Factory
<CustomDropdown@DropDown>:
id: dropdown
on_select:
app.root.ids.btn.text = '{}'.format(args[1])
self.dismiss()
Button:
id: btn1
text: 'option 1'
size_hint_y: None
height: '48dp'
on_release:
dropdown.select(btn1.text)
Button:
id: btn2
text: 'option 2'
size_hint_y: None
height: '48dp'
on_release:
dropdown.select(btn2.text)
<MyGridLayout>:
rows: 2
id: main_win
Button:
id: btn
text: 'Press'
on_release: Factory.CustomDropdown().open(self)
size_hint_y: None
height: '48dp'
BoxLayout:
BoxLayout: