为了账号安全,请及时绑定邮箱和手机立即绑定

Kivy 展开和折叠面板以隐藏内部元素

Kivy 展开和折叠面板以隐藏内部元素

慕桂英546537 2022-10-18 17:17:39
我是 kivy 的新手,我正在尝试创建一个小部件以在单击按钮后折叠和展开。当它被折叠时,内部小部件必须被隐藏,当它被展开时,高度应该是可能的最小高度。我做了这个实现,但是当展开时我无法正确设置高度,并且孩子们在折叠时不会隐藏......我知道在 kivyMD 库中已经有一个小部件可以执行此操作,但我需要创建一个不同的按钮和儿童的布局,所以如果有人可以帮助我......kivy 文件:teeeste.kv#:import C kivy.utils.get_color_from_hex<CLabel@Label>:    color: 0,0,0,1    size_hint_y: None    height: 40<CButton@Button>:    background_color: 0,0,0,0    canvas.before:        Color:            rgba: C("#455A64") if self.state == "normal" else (0.26,0.26,0.26,0.85)        RoundedRectangle:            pos: self.pos            size: self.size            radius: 10,10,10,10    size_hint: None,None    size: 300,40<Box@GridLayout>:    canvas.before:        Color:            rgba: 1,1,1,1        RoundedRectangle:            size: self.size            pos: self.pos            radius: 10,10,10,10    cols: 1    size_hint: None,None    width: 300    height: self.minimum_height    orientation: "tb-lr"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: box.height = 300 if box.height == 40 else 40            CLabel:                text: "abc"            CLabel:                text: "abc"            CLabel:                text: "abc"            CLabel:                text: "abc"蟒蛇文件:# coding: utf-8from kivy.app import Appfrom kivy.core.window import Windowfrom kivy.lang import Builderfrom kivy.utils import get_color_from_hexWindow.clearcolor = get_color_from_hex("#424242")class TesteApp(App):    def build(self):        return aplicativoaplicativo = Builder.load_file("teeeste.kv")if __name__ == "__main__":    TesteApp().run()我进行了@JohnAnderson 建议的更改,并尝试在彼此内部实现其他 Box,但有时在单击“NUMBER 3”按钮后,相关框内的子项会停止展开。这是实际的代码:
查看完整描述

1 回答

?
慕妹3146593

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)


查看完整回答
反对 回复 2022-10-18
  • 1 回答
  • 0 关注
  • 118 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号