Kivy展开和折叠一个面板,以隐藏内部结构。

我是kivy的新手,我想创建一个widget,在点击按钮后收拢和展开。当它收拢时,里面的小部件必须隐藏,而当它扩展时,高度应该是最小的高度.我做了这样的实现,但当扩展时,我不能正确设置高度,并且当收拢时,孩子们不隐藏。

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"

python文件。

# 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的建议进行了修改,并且我试着在每个盒子里面实现了其他的盒子,但是有时候在点击 “NUMBER 3 “按钮后,相关盒子里面的孩子就停止了展开。

kivy: teeeste.ky

#:import C kivy.utils.get_color_from_hex
<CLabel@Label>:
    color: 0,0,0,1
    size_hint_y: None
    height: 40

<CButton>:
    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: 1.0,None
    height: 40

<Box@GridLayout>:
    canvas.before:
        Color:
            rgba: 1,1,1,1
        RoundedRectangle:
            size: self.size
            pos: self.pos
    size_hint: 0.8,None
    height: self.minimum_height

FloatLayout:
    AnchorLayout:
        anchor_x: "center"
        anchor_y: "top"
        padding: 0,30,0,0
        ScrollView:
            do_scroll_x: False
            size_hint: None, 0.9
            width: 300
            GridLayout:
                cols: 1
                spacing: 5
                size_hint_y: None
                height: self.minimum_height
                Box:
                    id: box
                    cols: 1
                    CButton:
                        text: "NUMBER 1"
                        on_release: self.exp_or_collapse(box)
                    CLabel:
                        text: "aaa"
                Box:
                    id: box2
                    cols: 1
                    CButton:
                        text: "NUMBER 2"
                        on_release: self.exp_or_collapse(box2)
                    CLabel:
                        text: "bbb"
                Box:
                    id: box3
                    cols: 1
                    CButton:
                        text: "NUMBER 03"
                        on_release: self.exp_or_collapse(box3)
                    Box:
                        id: box4
                        cols: 1
                        CButton:
                            text: "CHILD 1"
                            on_release: self.exp_or_collapse(box4)
                        CLabel:
                            text: "111"
                    Box:
                        id: box5
                        cols: 1
                        CButton:
                            text: "CHILD 2"
                            on_release: self.exp_or_collapse(box5)
                        CLabel:
                            text: "222"
                    Box:
                        id: box6
                        cols: 1
                        CButton:
                            text: "CHILD 3"
                            on_release: self.exp_or_collapse(box6)
                        CLabel:
                            text: "333"
                    Box:
                        id: box7
                        cols: 1
                        CButton:
                            text: "CHILD 4"
                            on_release: self.exp_or_collapse(box7)
                        CLabel:
                            text: "444"
                    Box:
                        id: box8
                        cols: 1
                        CButton:
                            text: "CHILD 5"
                            on_release: self.exp_or_collapse(box8)
                        CLabel:
                            text: "555"
                    Box:
                        id: box9
                        cols: 1
                        CButton:
                            text: "CHILD 6"
                            on_release: self.exp_or_collapse(box9)
                        CLabel:
                            text: "666"
                    Box:
                        id: box10
                        cols: 1
                        CButton:
                            text: "CHILD 7"
                            on_release: self.exp_or_collapse(box10)
                        CLabel:
                            text: "777"
                    Box:
                        id: box11
                        cols: 1
                        CButton:
                            text: "CHILD 8"
                            on_release: self.exp_or_collapse(box11)
                        CLabel:
                            text: "888"
                    Box:
                        id: box12
                        cols: 1
                        CButton:
                            text: "CHILD 9"
                            on_release: self.exp_or_collapse(box12)
                        CLabel:
                            text: "999"

Python文件:main.py

import kivy
kivy.require("1.11.0")
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button


class CButton(Button):
    def exp_or_collapse(self, id):
        if id.height == self.height:
            # expand:
            for child in id.children:
                child.height = 40
                child.opacity = 1
        else:
            # collapse
            for child in id.children:
                if child != self:
                    child.height = 0
                    child.opacity = 0


class expApp(App):
    def build(self):
        return Builder.load_file("exp.kv")


if __name__ == "__main__":
    expApp().run()

想象中的所有盒子都是折叠的

解决方案:

一种方法是在函数中写一个方法来实现它。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జజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజ Label 将显示其 text 即使其大小为0。

上面的代码设计为只处理 CLabelsCButtons 在…中 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)

给TA打赏
共{{data.count}}人
人已打赏
解决方案

LinkedIn工作搜索REST API问题

2022-4-21 13:08:46

解决方案

Django url显示错误:未解决的引用。

2022-4-21 13:08:48

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索