【问题标题】:How to get Kivy accordion to collapse on press and how to set default accordion collapse setting如何让 Kivy 手风琴在按下时折叠以及如何设置默认手风琴折叠设置
【发布时间】:2014-09-25 21:13:40
【问题描述】:

我们在 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'

谢谢

【问题讨论】:

    标签: python python-2.7 kivy


    【解决方案1】:

    我不确定如何解决您的第一个问题,但我所做的让手风琴在展开位置开始的方法是在我的第一个手风琴项目上使用折叠:错误标志。

    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'
    

    【讨论】:

      【解决方案2】:

      所以,我意识到这个问题已经有 5 年历史了,但我今天自己遇到了这个问题,并且能够通过创建自定义 AccordionAccordionItem 类来让它工作(至少对于我正在做的事情),并覆盖Accordion._do_layoutAccordionItem.on_touch_down。我只是从它们的基类中复制并粘贴了这些方法,并做了一些如下所述的小改动。

      # 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()
      
      # file: popupaccordion.kv
      
      [PopUpAccordionItemTitle@Label]:
          text: ctx.title
          normal_background: ctx.item.background_normal if ctx.item.collapse else ctx.item.background_selected
          disabled_background: ctx.item.background_disabled_normal if ctx.item.collapse else ctx.item.background_disabled_selected
          canvas.before:
              Color:
                  rgba: self.disabled_color if self.disabled else self.color
              BorderImage:
                  source: self.disabled_background if self.disabled else self.normal_background
                  pos: self.pos
                  size: self.size
              PushMatrix
              Translate:
                  xy: self.center_x, self.center_y
              Rotate:
                  angle: 90 if ctx.item.orientation == 'horizontal' else 0
                  axis: 0, 0, 1
              Translate:
                  xy: -self.center_x, -self.center_y
          canvas.after:
              PopMatrix
      
      <PopUpAccordion>:
          pos_hint: {"center_x": 0.5, "y": 0}
          orientation: "vertical"
          PopUpAccordionItem:
              title: "test1"
              Label:
                  text: "test item 1"
          PopUpAccordionItem:
              title: "test2"
              Label:
                  text: "test item 2"
      

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

      【讨论】:

        猜你喜欢
        • 2012-04-26
        • 1970-01-01
        • 2011-10-01
        • 2012-11-21
        • 1970-01-01
        • 2013-05-29
        • 2016-02-28
        • 1970-01-01
        • 2017-08-30
        相关资源
        最近更新 更多