简体   繁体   中英

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

I wrote a kiwi app that has two pages. The first page contains only one button to go to the next page. The second page is a list of icons and titles. It worked well when the app had a page, but I get an error adding the first page as a menu. How can I solve it? The error is for line 51 that I specified. Error: '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()

You are trying to access the text_container id in the App , but that id is in the CoinScreen , not in the App . You can adjust the on_start() method to access the CoinScreen , like this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM