【问题标题】:Dynamically add Grid Layouts to another layout created in .kv将网格布局动态添加到在 .kv 中创建的另一个布局
【发布时间】:2020-04-15 00:46:44
【问题描述】:

我希望能够从 .py 文件向我的滚动列表中添加一行。我想将每一行创建为一个对象,这样它就可以拥有像“user_id”这样的数据,它拥有但不显示(也可以采用不同的方法)。另外,请记住,滚动布局是在清除屏幕中创建的。请帮忙。我真的需要它。

来自我的screens.py:

class PurgeScreen(Screen):
    def backButton(self):
        SCREEN_MANAGER.current = 'dashboard'


class ListRow(PurgeScreen):
    def __init__(self, username, userid, profile_pic):
        self.username = username
        self.userid = userid
        self.profile = profile_pic

    def create_row(self):
        layout = GridLayout(row=1, row_force_default=True, row_default_height=40)
        layout.add_widget(Label(text=self.username))
        # Im going to add more stuff in the future
        self.ids.widget_list.add_widget(layout)

来自我的 purge.kv:

<PurgeScreen>:
    name: 'purge'
    FloatLayout:
        size_hint: None, None
        ScrollView:
            size_hint: (None, None)
            do_scroll_x: False
            bar_width: 4
            bar_inactive_color: (.7, .7, .7, .4)
            size: (root.width * .75, root.height * .65)
            x: (root.width * .5) - (self.width/2)
            y: root.height * 0.2
            GridLayout:
                id: widget_list
                cols: 1
                size_hint_y: 2
                height: self.minimum_height

                Button:
                    text: "1"

                Button:
                    text: "2"

                Button:
                    text: "3"

                Button:
                    text: "4"

【问题讨论】:

  • 如果将 create_row() 方法移到 PurgeScreen 类中,它应该可以工作。

标签: python oop kivy grid-layout kivy-language


【解决方案1】:

代码看起来不错,但是

  1. 什么时候调用create_row方法?
  2. 您是否有一个小部件(按钮或其他东西)会触发此方法以将小部件添加到 GridLayout 中?

【讨论】:

  • 我今天确实设法让它工作了。问题是我需要对 helpers.py 中的对象有一个海洋,所以我将 self 作为参数传递并用它来回调 PurgeScreen 类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多