【发布时间】:2016-06-22 16:34:10
【问题描述】:
我无法通过 root.ids.created_in_kv.created_in_py 在绑定到按钮的方法中访问动态创建的子项。当我检查 root.ids.created_in_kv.ids 字典时它是空的,但是 root.ids.created_in_kv.children
中有孩子我想要实现的是创建一个充当多选器的弹出窗口。它将接受可能的选择并动态创建标签-复选框对并将其添加到弹出内容中,并且在“应用”按钮上它将仅返回选择的列表(str())。
我无法在 kv 中构建包含多个小部件的弹出窗口,但以下工作(建议使其“更好”而不是受欢迎):
kv代码:
<SelectorPopup>:
title: 'empty'
BoxLayout:
id: inside
orientation: 'vertical'
BoxLayout:
id: options
BoxLayout:
id: buttons
orientation: 'vertical'
Button:
text: 'Apply'
on_release: root.return_selected()
Button:
text: 'Cancel'
on_release: root.dismiss()
<LabeledCheckbox@BoxLayout>:
id: entity
CheckBox:
id: choice
Label:
text: root.id
我正在创建标签-复选框对(打包在 GridLayout 中)并将其放入 options BoxLayout 的 python 代码:
class SelectorPopup(Popup):
def return_selected(self):
selected=[]
a = self.ids.inside.options.choices.ids # dict is empty
for item in a.keys():
selected.append(item) if a[item].ids.choice.value #add if checkbox checked
return selected
class MultiselectForm(BoxLayout):
data = ListProperty([])
def __init__(self, **kwargs):
super(MultiselectForm, self).__init__(**kwargs)
self.prompt = SelectorPopup()
def apply_data(self):
# write data to self.data
pass
def create_popup(self):
try:
# some code to check if choices are already created
check = self.prompt.ids.inside.options.choices.id
except AttributeError:
possible = ['choice1','choice2','choice3'] #query db for possible instances
choices = GridLayout(id='choices',cols=2)
for entity in possible:
choice = Factory.LabeledCheckbox(id=entity)
choices.add_widget(choice)
self.prompt.ids.options.add_widget(choices)
self.prompt.open()
问题:
1) 如何使 return_selected 方法起作用?
2) 有没有办法更好地构建弹出窗口?我无法将小部件树添加到 content ObjectProperty 中,例如:
<MyPopup>:
content:
BoxLayout:
Label:
text: 'aaa'
Label:
text: 'bbb'
【问题讨论】:
标签: python properties popup kivy