'super' object 在 kivymd 中没有属性 '__getattr__'

'super' object has no attribute '__getattr__' in kivymd

我写了一个有两个页面的 kiwi 应用程序。 第一页只有一个按钮可以转到下一页。 第二页是图标和标题列表。 当应用程序有一个页面时它运行良好,但我在将第一页添加为菜单时出错。 我该如何解决? 错误是针对我指定的第 51 行。 错误: 'super' object has no attribute '__getattr__'

from kivy.lang import Builder
import glob
from kivymd.app import MDApp
from kivymd.uix.list.list import  TwoLineAvatarListItem
from kivymd.uix.list.list import ImageLeftWidget
from kivy.uix.screenmanager import ScreenManager, Screen

KV = '''
ScreenManager:
    MenuScreen:
    CoinScreen:
<MenuScreen>:
    name:"menu"
    MDRoundFlatButton:
        text: "SelectCoin"
        pos_hint: {"center_x": .2, "center_y": .8}
        on_press: root.manager.current = 'select_coin'

<CoinScreen>:
    name:"select_coin"
    ScrollView:
        MDList:
            id: text_container
            
'''
class MenuScreen(Screen):
    pass


class CoinScreen(Screen):
    pass

sm = ScreenManager()
sm.add_widget(MenuScreen(name='main'))
sm.add_widget(CoinScreen(name='select_coin'))


class Test(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def on_start(self):
        icon_path = glob.glob("icon/*.png")
        for _ in range(len(icon_path)):
            icon_path[_]=icon_path[_].replace("\","//")
        
        for i in icon_path:
            icons = ImageLeftWidget(source=i)
            items = TwoLineAvatarListItem(text=i + ' item',secondary_text= "Secondary text here")
            items.add_widget(icons)
            self.root.ids.text_container.add_widget(items) #**********error**********


Test().run()

您正在尝试访问 App 中的 text_container ID,但该 ID 在 CoinScreen 中,而不是在 App 中。您可以调整 on_start() 方法来访问 CoinScreen,像这样:

class Test(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def on_start(self):
        icon_path = glob.glob("icon/*.png")
        for _ in range(len(icon_path)):
            icon_path[_] = icon_path[_].replace("\", "//")

        coin_screen = MDApp.get_running_app().root.get_screen('select_coin')
        for i in icon_path:
            icons = ImageLeftWidget(source=i)
            items = TwoLineAvatarListItem(text=i + ' item', secondary_text="Secondary text here")
            items.add_widget(icons)
            coin_screen.ids.text_container.add_widget(items)