1 回答

TA贡献1820条经验 获得超9个赞
一种方法是在类中编写一个方法来做到这一点CButton:
class CButton(Button):
def exp_or_collapse(self, box):
if box.height == self.height:
# expand
for child in box.children:
child.height = 40
child.opacity = 1
else:
# collapse
for child in box.children:
if child != self:
child.height = 0
child.opacity = 0
然后在kv文件中使用它:
FloatLayout:
AnchorLayout:
anchor_x: "center"
anchor_y: "top"
padding: 0,30,0,0
Box:
id: box
CButton:
text: "Press to expand or colapse"
on_release: self.exp_or_collapse(box)
CLabel:
text: "abc"
CLabel:
text: "abc"
CLabel:
text: "abc"
CLabel:
text: "abc"
需要不透明度调整才能完全隐藏CLabels,因为即使它的大小为 0 ,aLabel也会显示它。text
上面的代码旨在仅处理CLabels和CButtons在Box. 要将其扩展为处理 的一般子级Box,exp_or_collapse()可以将 修改为:
class CButton(Button):
removedChildren = ListProperty([])
def exp_or_collapse(self, id):
if len(self.removedChildren) > 0:
# expand:
# re-add all children
self.removedChildren.reverse()
for child in self.removedChildren:
id.add_widget(child)
self.removedChildren = []
else:
# collapse
# remove all children (except ourself)
for child in id.children:
if child != self:
self.removedChildren.append(child)
for child in self.removedChildren:
id.remove_widget(child)
添加回答
举报