Kivy以编程方式将项目添加到手风琴

Kivy add items to accordion programmatically

我有一个简单的选项卡式应用程序。 现在在每个选项卡中我都会有手风琴

我已经在 *.kv 文件中创建了布局。 现在我的问题是(可能是一个愚蠢的问题,但我对 kivy 的工作原理还很陌生):如何在我创建的手风琴中添加项目和子项目。

在这个例子中,我正在构建一个电视观看应用程序。 因此,例如 Live Tv 将手风琴标题作为频道组和频道内部(我将从数据库中获取)

这是我的 python 代码:

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.config import Config
from kivy.uix.accordion import Accordion, AccordionItem


class LiveTV(Accordion):
    def ListItems(self):
        root = Accordion()
        for x in range(1, 3, 1):
            item = AccordionItem(title='Page % d' % x)
            root.add_widget(item)
        return root


class Display(TabbedPanel):
    def __init__(self, **kwargs):
        super(Display, self).__init__(**kwargs)
        rootAc = LiveTV.ListItems(self)


class IptvApp(App):
    def build(self):
        Config.set('graphics', 'width', '1366')
        Config.set('graphics', 'height', '768')
        Config.set('graphics', 'resizable', False)
        # Change to auto for full screen app
        Config.set('graphics', 'fullscreen', '0')
        # Maximized for full resolution app
        Config.set('graphics', 'window_state', 'visible')
        # To remove right click orange dot (touch emulation)
        Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
        Config.write()
        return Display()


if __name__ == '__main__':
    IptvApp().run()

这是我的 *.kv 文件

<Button>:
font_size: 40
color:0.3,0.6,0.7,1
size_hint: 0.4, 0.05

<Display>
    tab_width: 600
    size_hint: 1, 1
    do_default_tab: False

    TabbedPanelItem:
        text: 'Live TV'
        FloatLayout:
            Accordion:
                size_hint: 0.3,0.2
                pos_hint: {"top": 1}
                orientation: 'vertical'
                AccordionItem:
                    title: 'Plot'
                AccordionItem:
                    title: 'Number'
                AccordionItem:
                    title: 'Another number'

    TabbedPanelItem:
        text: 'Video On Demand'
        FloatLayout:
            Label:
                text: 'Second tab content area'
            Button:
                pos_hint: {"top": 0.4, "right": 0.7}
                text: 'Button that does nothing'

    TabbedPanelItem:
        text: 'Series'

    TabbedPanelItem:
        text: 'Settings'

显示如下:

如何以编程方式定位我在 *.kv 文件中所做的手风琴,以便我可以填充项目和子项目?

此致,

好的,我明白了。对于所有来到这里的人来说,这就是它的完成方式:

在 kv 文件中,您要以编程方式定位的元素添加一个新的 属性 id,例如

Button:
    id: btn_submit

并且在父 "class" 中,在我的例子中 <Display> 定义该元素,即

<Display>
    btnSubmit: btn_submit

现在在 python 代码中定义了 class 显示,您可以通过简单地编写以下内容来定位按钮:self.btnSubmit.* 例如,您可以 add_widget 或任何您想要的内容。