Skip to main content
 首页 » 编程设计

python之如何让 Kivy Accordion 在按下时折叠以及如何设置默认 Accordion 折叠设置

2025年05月04日9java哥

我们在 Kivy 应用的主菜单中使用 Accordion ,但遇到了两个问题:

1) 当我们打开一个 Accordion 项目时,当我们再次按下该项目时,该项目不会折叠。让它崩溃的唯一方法是按下另一个 Accordion 项目。这仅仅是 Kivy Accordion 小部件的工作方式还是有办法改变此设置?

2) 当我们的屏幕打开时,列表中的最后一个 Accordion 项目从一开始就展开了。我们如何让这个 Accordion 项目加载到折叠位置?我们尝试在我们的 kv 文件中设置 collapse: True 但这没有用

我们的kv代码如下:

GeneralBoxLayout: 
    GridLayout1: 
    BodyBoxLayout: 
        rows: 2 
        GeneralTextGridLayout: 
            size_hint: (1,.07) 
            GeneralTextLabel: 
                text: '[color=0046C3]Select a topic[/color]' 
        ScrollView: 
            size_hint: (1,.93) 
            HomeGridLayout: 
                Accordion: 
                    orientation: "vertical" 
                    AccordionItem: 
                        title: "Topic 1" 
                        background_normal: 'img/blue_button5.png' 
                        background_selected: 'img/blue_button5.png' 
                        size_hint_y: None 
                        height: '50dp' 
                        font_size: '12sp' 
                        border: 20, 20, 20, 20 
                        GameButton0: 
                            text: 'Game 1' 
 
                    AccordionItem: 
                        title: 'Topic 2' 
                        background_normal: 'img/blue_button5.png' 
                        background_selected: 'img/blue_button5.png' 
                        GameButton0: 
                            text: 'Game 1' 
 
                    AccordionItem: 
                        title: 'Topic 3' 
                        background_normal: 'img/blue_button5.png' 
                        background_selected: 'img/blue_button5.png' 
                        GameButton0: 
                            text: 'Game 1' 
 
                    AccordionItem: 
                        title: 'Topic 4' 
                        background_normal: 'img/blue_button5.png' 
                        background_selected: 'img/blue_button5.png' 
                        GameButton0: 
                            text: 'Game 1' 
 
                    AccordionItem: 
                        title: 'Topic 5' 
                        background_normal: 'img/blue_button5.png' 
                        background_selected: 'img/blue_button5.png' 
                        GameButton0: 
                            text: 'Game 1' 
 
                    AccordionItem: 
                        title: 'Topic 6' 
                        background_normal: 'img/blue_button5.png' 
                        background_selected: 'img/blue_button5.png' 
                        GameButton0: 
                            text: 'Game 1' 
 
                    AccordionItem: 
                        title: 'Topic 7' 
                        background_normal: 'img/blue_button5.png' 
                        background_selected: 'img/blue_button5.png' 
                        GameButton0: 
                            text: 'Game 1' 
 
                    AccordionItem: 
                        title: 'Topic 8' 
                        background_normal: 'img/blue_button5.png' 
                        background_selected: 'img/blue_button5.png' 
                        GameButton0: 
                            text: 'Game 1' 
 
                    AccordionItem: 
                        title: 'Topic 9' 
                        background_normal: 'img/blue_button5.png' 
                        background_selected: 'img/blue_button5.png' 
                        collapse: True 
                        GameButton0: 
                            text: ' Game 1' 
 
    FooterGridLayout: 
        ReturnButton: 
            text: 'Logout' 

谢谢

请您参考如下方法:

我不确定如何解决您的第一个问题,但我为使 Accordion 在展开位置开始而做的是对我的第一个 Accordion 项目使用折叠:错误标志。

MyAccordion: 
        orientation: 'vertical' 
 
        MyItem:           #The the top accordion item that needs to open expanded 
            title: 'First Item' 
            collapse: False 
 
        MyItem:           #The the next accordion item, will be collapsed 
            title: 'Second Item' 
 
        MyItem:           #The the next accordion item, will also be collapsed 
            title: 'Third Item'