如何让Kivy手风琴折叠在按下和如何设置默认手风琴折叠设置

2024-05-29 08:09:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我们正在Kivy应用程序的主菜单中使用手风琴,并正在解决两个问题:

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

2)当我们的屏幕打开时,列表中的最后一个手风琴项目从开始就显示为展开。我们如何使这个手风琴项目装载在一个折叠的位置?我们尝试在我们的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'

谢谢


Tags: 项目textgameimgtopicpngtitleblue
2条回答

我不知道如何解决你的第一个问题,但我做了什么,使手风琴开始在扩大的位置是使用一个折叠:假标志我的第一个手风琴项目。在

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'

所以,我意识到这个问题已经有5年了,但我今天自己也遇到了这个问题,通过创建自定义的Accordion和{}类,重写Accordion._do_layout和{},我就能够让它工作了(至少对于我正在做的工作)。我只是从它们的基类复制并粘贴了这些方法,并做了一些下面提到的小更改。在

# file: popupaccordion.py
from kivy.app import App
from kivy.uix.accordion import Accordion, AccordionItem
from kivy.logger import Logger

class PopUpAccordion(Accordion):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def _do_layout(self, dt):
        children = self.children
        if children:
            all_collapsed = all(x.collapse for x in children)
        else:
            all_collapsed = False

        # Changed below: if all items are collapsed, do nothing. This is what we want.
        # if all_collapsed:
        #     children[0].collapse = False

        orientation = self.orientation
        min_space = self.min_space
        min_space_total = len(children) * self.min_space
        w, h = self.size
        x, y = self.pos
        if orientation == 'horizontal':
            display_space = self.width - min_space_total
        else:
            display_space = self.height - min_space_total

        if display_space <= 0:
            Logger.warning('Accordion: not enough space '
                           'for displaying all children')
            Logger.warning('Accordion: need %dpx, got %dpx' % (
                min_space_total, min_space_total + display_space))
            Logger.warning('Accordion: layout aborted.')
            return

        if orientation == 'horizontal':
            children = reversed(children)

        for child in children:
            child_space = min_space
            child_space += display_space * (1 - child.collapse_alpha)
            child._min_space = min_space
            child.x = x
            child.y = y
            child.orientation = self.orientation
            if orientation == 'horizontal':
                child.content_size = display_space, h
                child.width = child_space
                child.height = h
                x += child_space
            else:
                child.content_size = w, display_space
                child.width = w
                child.height = child_space
                y += child_space

class PopUpAccordionItem(AccordionItem):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.title_template = 'PopUpAccordionItemTitle'

    def on_touch_down(self, touch):
        if not self.collide_point(*touch.pos):
            return
        if self.disabled:
            return True
        if self.collapse:
            self.collapse = False
            return True
        # Changed below: if item is not collapsed and user clicked the title bar, collapse.
        if not self.collapse and self.container_title.collide_point(*touch.pos):
            self.collapse = True
        return super(AccordionItem, self).on_touch_down(touch)

class PopUpAccordionApp(App):
    def build(self):
        return PopUpAccordion()

if __name__ == "__main__":
    PopUpAccordionApp().run()
^{pr2}$

请注意,kv文件包含kivy模板。我不确定这些是如何工作的,kivy模板是不推荐使用的。但是,如果您想更改打开/关闭PopUpAccordionItem的按钮的外观,这是(至少一种方法)可以做到的方法。还要注意,我们通过传递模板名的字符串值将PopUpAccordionItem连接到__init__中的模板。。。这看起来很奇怪但很管用。在

相关问题 更多 >

    热门问题