我正在使用 KivyMD 构建应用程序。我有一个用 MDLabels 制作列表的函数。列表初始化后,我想用最新数据“刷新”列表。因此,我使用 clear_widgets() 从列表中删除标签,然后用新数据重建列表。
问题是,一旦列表正在重建,间距似乎会受到干扰。我尝试了很多方法,但我无法弄清楚在这种情况下问题出在哪里。
有人知道这里出了什么问题吗?我在下面复制了一个简单的例子。 非常感谢!
kivy 版本 = 2.0.0, kivymd 版本 = 0.104.1,python 3.7
main.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
class MainApp(MDApp):
def build(self):
screen = Builder.load_file('kivy.kv')
return screen
def make_list(self):
self.root.ids.button.text = 'Click me again'
if self.root.ids.list.children:
self.root.ids.list.clear_widgets()
if not self.root.ids.list.children:
for i in range(9):
self.root.ids.list.add_widget(MDLabel(text='Label' + str(i)))
MainApp().run()
.kv 文件
Screen:
BoxLayout:
orientation: 'horizontal'
FloatLayout:
MDLabel:
text: 'Click the button below for a list'
halign: 'center'
pos_hint: {'center_x': .5, 'center_y': .6}
MDFlatButton:
id: button
text: 'button'
pos_hint: {'center_x': .5, 'center_y': .4}
on_release: app.make_list()
FloatLayout:
MDList:
id: list
spacing: 40
pos_hint: {'center_x': .7, 'center_y': .6}
答案 0 :(得分:0)
很难解读 MDList
和 MDLabel
如何计算它们的高度,但您可以通过指定每个 height
的 MDLabel
来解决您的问题。一种方法是这样的:
self.root.ids.list.add_widget(MDLabel(text='Label' + str(i), size_hint_y=None, height=15))