将网格布局动态添加到在.kv中创建的另一个布局中



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

从我的屏幕。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)

来自我的清除。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"

代码看起来不错,但

  1. 何时调用方法create_row
  2. 你有一个小部件(按钮或其他什么(可以触发这个方法将小部件添加到GridLayout中吗

最新更新