【问题标题】:Kivy add items to accordion programmaticallyKivy 以编程方式将项目添加到手风琴
【发布时间】:2020-05-31 22:04:28
【问题描述】:

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

我在 *.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 文件中所做的手风琴,以便我可以填充项目和子项目?

问候,

【问题讨论】:

  • 您必须为您的Accordion 添加一个id(例如id: channels),然后通过app.root.ids.channels 访问它..
  • 你能解释一下我的例子吗
  • 您必须将添加小部件的部分添加到您的代码中,这样我们才能看到您想要做什么(以及如何做)..
  • 如果你想办法把我推向正确的方向! @noEmbryo

标签: python python-3.x kivy accordion


【解决方案1】:

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

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

Button:
    id: btn_submit

在我的情况下,在父“类”&lt;Display&gt; 中定义该元素,即

<Display>
    btnSubmit: btn_submit

现在在定义了 Display 类的 python 代码中,您可以通过简单地编写来定位按钮:self.btnSubmit.* 例如,您可以添加_widget 或任何您想要的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 2013-10-26
    • 1970-01-01
    • 2013-12-25
    相关资源
    最近更新 更多