2

我有这个代码。如果我将 Accordion 项目的数量增加到超过一定数量,应用程序就会失控。gridLayout 版本(已注释掉)按预期工作。违规区域位于 HERE 和 THERE 之间。

from kivy.app import App
from kivy.uix.spinner import Spinner, SpinnerOption
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.accordion import Accordion, AccordionItem
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window

class MusicController(App):

    def build(self):
        b = BoxLayout(orientation='vertical')
        top = Spinner(text = '12', values = ["Songs","Artist","Playlist","Search"], background_color = (.3,.2,.5,1), size_hint_y = 0.1)

    #< HERE >#

        items = Accordion(orientation='vertical')
        for x in xrange(30):
            item = AccordionItem(title='Title %d' % x)
            buttons = BoxLayout()
            buttons.add_widget(Button(text='Play'))
            buttons.add_widget(Button(text='Pause'))
            buttons.add_widget(Button(text='Pause'))
            item.add_widget(buttons)
            items.add_widget(item)

        #~ items = GridLayout(cols=1, spacing=10, size_hint_y=None)
        #~ items.bind(minimum_height=items.setter('height'))
        #~ for i in range(30):
            #~ btn = Button(text=str(i), 

    #< THERE >#

        sv = ScrollView(do_scroll_x = False)
        sv.add_widget(items)
        now_playing = BoxLayout(size_hint_y = 0.1)
        for widget in ["Prev","Play","Pause","Next","NP"]:
            now_playing.add_widget(Button(text=widget))

        b.add_widget(top)
        b.add_widget(sv)
        b.add_widget(now_playing)
        return b

if __name__ == '__main__':
    MusicController().run()
4

1 回答 1

3

正如您控制台上的错误所暗示的那样

[WARNING] [Accordion   ] not enough space for displaying all children
[WARNING] [Accordion   ] need XXXX, got XXXXpx
[WARNING] [Accordion   ] layout aborted.

只需增加给 Accordian 的空间,这样它就可以容纳所有的孩子

将代码的第 19 行更改为

items = Accordion(orientation='vertical', size_hint_y=None, height=50*30)

应该管用。

在 GridLayout 中,您可以通过将 Gridlayout minimum_height(由子级组合的高度计算)绑定到它的高度来执行相同的操作。

items.bind(minimum_height=items.setter('height'))

Accordian 没有这种方便的方法,所以你必须自己做。

为了使其更具动态性,您可以像这样绑定到 Accordion 的 children 属性:

item.bind(children=self.update_size)

然后像这样定义 update_size::

def update_size(self, instance, value):
    # change 50 to what you want your AccordionItem height to be.
    instance.size = 50*len(instance.children)
于 2013-03-27T01:22:05.963 回答