KivyMD:如何自动显示在 python 文件中生成的 MDList

我是 Kivy/KivyMD/programming 的新手,我只是没有掌握 python 代码和 kv 代码之间的一些基本知识。


我正在构建一个简单的应用程序,它使用 ScreenManager 在两个屏幕之间切换。在每个屏幕上,我计划有一个动态生成的列表,其中包含来自数据库的数据。


我可以通过将它们放入 kv 文件来添加静态列表和其他小部件。但我似乎无法理解如何在 python 文件的类中创建/更新数据并将其链接到 kv 文件 ID。


在下面的代码中,程序运行正常,我在其中放置了一个成功生成列表的按钮,但目标是没有按钮并在应用程序启动时自动生成列表。


我已经对我尝试过的事情添加了一些评论。我是在使用错误的变量名还是在做一些根本错误的事情?


[main.py python 文件]


from kivymd.app import MDApp

from kivy.lang import Builder

from kivy.uix.screenmanager import ScreenManager, Screen

from kivymd.uix.list import OneLineListItem



class FirstWindow(Screen):

    print('This prints automatically when App launches')


    # 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()

[多屏.kv 文件]


WindowManager:

    FirstWindow:

        name: 'firstwindow'

    SecondWindow:

        name: 'secondwindow'



<FirstWindow>:

    BoxLayout:

        orientation: 'vertical'


        MDToolbar:

            title: 'SCREEN 1'


        Button:

            text: 'List maker button'

            on_release: root.button_push()


        ScrollView:

            MDList:

                id: list_one


        MDFloatingActionButton:

            elevation: 8

            icon: 'plus'

            pos_hint: {'center_x': .5}

            on_press:

                app.root.current = 'secondwindow'

                root.manager.transition.direction = 'left'


慕妹3146593
浏览 119回答 2
2回答

Smart猫小萌

您可以添加__init___方法来Clock.schedule_once触发列表创建。修改后的代码如下。我没有更改其余代码,只是添加了两个函数来显示自动列表创建。from kivymd.app import MDAppfrom kivy.lang import Builderfrom kivy.uix.screenmanager import ScreenManager, Screenfrom kivymd.uix.list import OneLineListItemfrom kivy.clock import Clockclass FirstWindow(Screen):&nbsp; &nbsp; print('This prints automatically when App launches')&nbsp; &nbsp; def __init__(self, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__(**kwargs)&nbsp; &nbsp; &nbsp; &nbsp; Clock.schedule_once(self.create_list)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def create_list(self, *args):&nbsp; &nbsp; &nbsp; &nbsp; for i in range (20):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ids.list_one.add_widget(OneLineListItem(text=f'List Item {i}'))&nbsp; &nbsp; # But adding widgets doesn't happen automatically&nbsp; &nbsp; # I tried variations but the variable is always not defined&nbsp; &nbsp; #self.ids.list_one.add_widget(OneLineListItem(text='List Item 1'))&nbsp; &nbsp; #root.ids.list_one.add_widget(OneLineListItem(text='List Item 1'))&nbsp; &nbsp; #ids.list_one.add_widget(OneLineListItem(text='List Item 1'))&nbsp; &nbsp; # This function works when called from a button&nbsp; &nbsp; def button_push(self):&nbsp; &nbsp; &nbsp; &nbsp; for i in range (20):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ids.list_one.add_widget(OneLineListItem(text=f'List Item {i}'))class SecondWindow(Screen):&nbsp; &nbsp; passclass WindowManager(ScreenManager):&nbsp; &nbsp; passclass MultiscreenApp(MDApp):&nbsp; &nbsp; def build(self):&nbsp; &nbsp; &nbsp; &nbsp; return Builder.load_file('Multiscreen.kv')if __name__ == '__main__':&nbsp; &nbsp; MultiscreenApp().run()

慕莱坞森

class FirstWindow(Screen):&nbsp; &nbsp; def on_enter(self, *args):&nbsp; &nbsp; &nbsp; &nbsp; """Event fired when the screen is displayed: the entering animation is&nbsp; &nbsp; &nbsp; &nbsp; complete."""&nbsp; &nbsp; &nbsp; &nbsp; def on_enter(interval):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i in range (20):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ids.list_one.add_widget(OneLineListItem(text=f'List Item {i}'))&nbsp; &nbsp; &nbsp; &nbsp; Clock.schedule_once(on_enter)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python