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

我是 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-8


from kivy.app import App

from kivy.core.window import Window

from kivy.lang import Builder

from kivy.utils import get_color_from_hex



Window.clearcolor = get_color_from_hex("#424242")



class TesteApp(App):

    def build(self):

        return aplicativo



aplicativo = Builder.load_file("teeeste.kv")


if __name__ == "__main__":

    TesteApp().run()

我进行了@JohnAnderson 建议的更改,并尝试在彼此内部实现其他 Box,但有时在单击“NUMBER 3”按钮后,相关框内的子项会停止展开。这是实际的代码:


慕桂英546537
浏览 119回答 1
1回答

慕妹3146593

一种方法是在类中编写一个方法来做到这一点CButton:class CButton(Button):&nbsp; &nbsp; def exp_or_collapse(self, box):&nbsp; &nbsp; &nbsp; &nbsp; if box.height == self.height:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # expand&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for child in box.children:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child.height = 40&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child.opacity = 1&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # collapse&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for child in box.children:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if child != self:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child.height = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child.opacity = 0然后在kv文件中使用它:FloatLayout:&nbsp; &nbsp; AnchorLayout:&nbsp; &nbsp; &nbsp; &nbsp; anchor_x: "center"&nbsp; &nbsp; &nbsp; &nbsp; anchor_y: "top"&nbsp; &nbsp; &nbsp; &nbsp; padding: 0,30,0,0&nbsp; &nbsp; &nbsp; &nbsp; Box:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: box&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CButton:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: "Press to expand or colapse"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; on_release: self.exp_or_collapse(box)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CLabel:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: "abc"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CLabel:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: "abc"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CLabel:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: "abc"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CLabel:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: "abc"需要不透明度调整才能完全隐藏CLabels,因为即使它的大小为 0 ,aLabel也会显示它。text上面的代码旨在仅处理CLabels和CButtons在Box. 要将其扩展为处理 的一般子级Box,exp_or_collapse()可以将 修改为:class CButton(Button):&nbsp; &nbsp; removedChildren = ListProperty([])&nbsp; &nbsp; def exp_or_collapse(self, id):&nbsp; &nbsp; &nbsp; &nbsp; if len(self.removedChildren) > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # expand:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # re-add all children&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.removedChildren.reverse()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for child in self.removedChildren:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id.add_widget(child)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.removedChildren = []&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # collapse&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # remove all children (except ourself)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for child in id.children:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if child != self:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.removedChildren.append(child)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for child in self.removedChildren:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id.remove_widget(child)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python