亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

KivyMD:如何自動顯示在 python 文件中生成的 MDList

KivyMD:如何自動顯示在 python 文件中生成的 MDList

慕妹3146593 2023-06-13 14:43:18
我是 Kivy/KivyMD/programming 的新手,我只是沒有掌握 python 代碼和 kv 代碼之間的一些基本知識。我正在構建一個簡單的應用程序,它使用 ScreenManager 在兩個屏幕之間切換。在每個屏幕上,我計劃有一個動態生成的列表,其中包含來自數據庫的數據。我可以通過將它們放入 kv 文件來添加靜態列表和其他小部件。但我似乎無法理解如何在 python 文件的類中創建/更新數據并將其鏈接到 kv 文件 ID。在下面的代碼中,程序運行正常,我在其中放置了一個成功生成列表的按鈕,但目標是沒有按鈕并在應用程序啟動時自動生成列表。我已經對我嘗試過的事情添加了一些評論。我是在使用錯誤的變量名還是在做一些根本錯誤的事情?[main.py python 文件]from kivymd.app import MDAppfrom kivy.lang import Builderfrom kivy.uix.screenmanager import ScreenManager, Screenfrom kivymd.uix.list import OneLineListItemclass 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):    passclass WindowManager(ScreenManager):    passclass 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'
查看完整描述

2 回答

?
Smart貓小萌

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


查看完整回答
反對 回復 2023-06-13
?
慕萊塢森

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)


查看完整回答
反對 回復 2023-06-13
  • 2 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號