1

谢谢参观。我是 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'

<SecondWindow>:
    BoxLayout:
        orientation: 'vertical'

        MDToolbar:
            title: 'SCREEN 2'

        ScrollView:
            MDList:
                id: list_two

        MDRaisedButton:
            text: 'Go Back'
            on_release:
                app.root.current = 'firstwindow'
                root.manager.transition.direction = 'right'
4

2 回答 2

2

您可以添加__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()
于 2020-08-23T08:40:33.117 回答
1
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)
于 2020-08-23T08:38:44.303 回答