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)
添加回答
舉報