亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號