如何在添加的小部件中添加新的小部件?

How to add New Widget in Added Widgets?

在“PageLayout”中,我想添加一个滚动网格(我有一个模板“ScrollGrid()”),我使用“add_grid”方法来实现。现在在每个“ScrollGrid()”或每个创建的页面中,我想添加新的小部件“EventTemplate()”。我该怎么做?
这个应用程序是一种“待办事项列表”,每一页都会对应每一天。是每天新建一个页面,然后在每个页面上加一些当天想做的事情,或者有什么简单的方法可以做到?
非常感谢!
Python:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView

class Page(BoxLayout):
    def add_grid(self):
        self.ids.page_id.add_widget(ScrollGrid())
    def swap_page_next(self):
        self.page = App.get_running_app().root.ids.page_id
        self.page.page = self.page.page + 1
    def swap_page_prev(self):
        self.page = App.get_running_app().root.ids.page_id
        self.page.page = self.page.page + -1
    def add_item(self):
        new_item = EventTemplate()
        App.get_running_app().root.ids.page_id ??????

class ScrollGrid(ScrollView):
    pass

class EventTemplate(BoxLayout):
    pass
class MyAppApp(App):
    pass

MyAppApp().run()

KV:

Page:
<Page>:
    orientation: "vertical"
    BoxLayout:
        size_hint: 1,0.1
        orientation: "horizontal"
        Button:
            text: "Prev"
            on_release: root.swap_page_prev()
        Button:
            text: "Next"
            on_release: root.swap_page_next()
    PageLayout:
        id: page_id
        border: 0
    BoxLayout:
        size_hint: 1, 0.1
        Button:
            text: "Add Page"
            on_release: root.add_grid()
        Button:
            text: "Add element in page"
            on_release: root.add_item()

<ScrollGrid>:
    GridLayout:
        cols: 1
        size_hint: 1, None
        height: self.minimum_height
        Button:
            text: "Button 1"
            size_hint_y: None
        Button:
            text: "Button 2"
            size_hint_y: None

<EventTemplate>:
    size_hint_y: None
    spacing: 1
    CheckBox:
        size_hint: 0.1, 1
    Label:
        text: "Some Event"

我建议将 "Add element in page" Button<Page> 移到 <ScrollGrid> 中。所以kv中的ScrollGrid规则可以是:

<ScrollGrid>:
    GridLayout:
        id: grid  # added id to allow easy access
        cols: 1
        size_hint: 1, None
        height: self.minimum_height
        Button:
            text: "Button 1"
            size_hint_y: None
        Button:
            text: "Add element in page"
            size_hint_y: None
            on_release: root.add_item()

并且 ScrollGrid class 变为:

class ScrollGrid(ScrollView):
    def add_item(self):
        new_item = EventTemplate()
        self.ids.grid.add_widget(new_item)

除了约翰安德森建议的其他答案外,我想添加以下方法。

    def add_item(self, page):
        new_item = EventTemplate()
        # Make sure page has widgets.
        if page.children:
            # Grab the current page index.
            index = page.page
            # Grab the ScrollView.
            scroll_grid = page.children[::-1][index]
            # Grab the only GridLayout of each ScrollView.
            container = scroll_grid.ids.grid
            # Now add the item.
            container.add_widget(new_item)

并且在 kvlang

...
    PageLayout:
        id: page_id
        border: 0
    BoxLayout:
        size_hint: 1, 0.1
        Button:
            text: "Add Page"
            on_release: root.add_grid()
        Button:
            text: "Add element in page"
            on_release: root.add_item(page_id) # Pass the PageLayout instance.

<ScrollGrid>:
    GridLayout:
        id: grid
        cols: 1
...

要显示任何日期的任何页面(可能已经作为数据保存在磁盘中),您只需要加载从该数据创建的单个实例。因此,您不需要同时包含和加载所有小部件(如果真的这样做可能会导致应用程序显着变慢)。

在这里您必须自己管理页面(或 page-index)以避免任何错误,这就是为什么在这种情况下我宁愿建议使用 Carousel 而不是因为您将拥有更多控制。