2 回答
TA贡献1911条经验 获得超7个赞
您可以添加__init___方法来Clock.schedule_once触发列表创建。修改后的代码如下。我没有更改其余代码,只是添加了两个函数来显示自动列表创建。
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.list import OneLineListItem
from kivy.clock import Clock
class FirstWindow(Screen):
print('This prints automatically when App launches')
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self.create_list)
def create_list(self, *args):
for i in range (20):
self.ids.list_one.add_widget(OneLineListItem(text=f'List Item {i}'))
# But adding widgets doesn't happen automatically
# I tried variations but the variable is always not defined
#self.ids.list_one.add_widget(OneLineListItem(text='List Item 1'))
#root.ids.list_one.add_widget(OneLineListItem(text='List Item 1'))
#ids.list_one.add_widget(OneLineListItem(text='List Item 1'))
# This function works when called from a button
def button_push(self):
for i in range (20):
self.ids.list_one.add_widget(OneLineListItem(text=f'List Item {i}'))
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class MultiscreenApp(MDApp):
def build(self):
return Builder.load_file('Multiscreen.kv')
if __name__ == '__main__':
MultiscreenApp().run()
TA贡献1810条经验 获得超4个赞
class FirstWindow(Screen):
def on_enter(self, *args):
"""Event fired when the screen is displayed: the entering animation is
complete."""
def on_enter(interval):
for i in range (20):
self.ids.list_one.add_widget(OneLineListItem(text=f'List Item {i}'))
Clock.schedule_once(on_enter)
添加回答
举报