"id usage in kivy:" AttributeError: 'super' object has no attribute '__getattr__'

"id usage in kivy:" AttributeError: 'super' object has no attribute '__getattr__'

简介: 所以,我正在尝试制作这个过滤掉 youtube 算法的 kivy 应用程序(基本上它让我只能看到我订阅的频道的视频)

问题

我正在尝试为我的订阅屏幕创建列表,以便我也可以列出我订阅的频道...我在“SubscriptionScreen”的“MDList”中使用了 ID“subs_id”

 ScrollView:
        MDList:
            id: subs_id

我正在尝试从“on_start”函数引用它,但我无法这样做并添加小部件列表项...(下面给出的错误)

整个代码:

from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager
from kivymd.uix.list import OneLineListItem

Window.size = (300, 500)

screen_helper = """
ScreenManager:
    HomeScreen:
    SubscriptionScreen:
    
<HomeScreen>:
    name: 'home'
    BoxLayout:
        orientation: 'vertical'
        MDToolbar:
            title: 'YouTube'
            left_action_items: [["youtube", lambda x: app.navigation_draw()]]
            right_action_items: [["magnify", lambda x: app.search_popup()]]
            MDIconButton:
                icon: "youtube-subscription"
                md_bg_color: app.theme_cls.primary_color
                pos_hint: {'center_x':0.5, 'center_y':0.5}   
                on_press: root.manager.current = "subscriptions"

        MDLabel:
            text: 'hello world'
            halign: 'center'
            
    MDRectangleFlatButton:
        text:'profile'
        pos_hint: {'center_x':0.5, 'center_y':0.5}   
        on_press: root.manager.current = "subscriptions"
        
<SubscriptionScreen>:
    name: 'subscriptions'
    BoxLayout:
        orientation: 'vertical'
        MDToolbar:
            title: 'YouTube'
            left_action_items: [["youtube", lambda x: app.navigation_draw()]]
            right_action_items: [["magnify", lambda x: app.search_popup()]]
            MDIconButton:
                icon: "youtube-subscription"
                md_bg_color: app.theme_cls.primary_color
                pos_hint: {'center_x':0.5, 'center_y':0.5}   
                on_press: root.manager.current = "profile"
        ScrollView:
            MDList:
                id: subs_id
                
                """



class HomeScreen(Screen):
    pass

class SubscriptionScreen(Screen):
    pass

sm = ScreenManager()
sm.add_widget(HomeScreen(name='home'))
sm.add_widget(SubscriptionScreen(name='subscriptions'))
sm.current = "home"


class DemoApp(MDApp):

    def build(self):
        self.theme_cls.primary_palette='Red'
        screen = Builder.load_string(screen_helper)
        return screen

    def on_start(self):
        for i in range(1,20):
            item = OneLineListItem(text="krish naik")
            self.root.ids.subs_id.add_widget(item)


DemoApp().run()

什么需要维修?

 self.root.ids.subs_id.add_widget(item)

Error/output:

    [INFO   ] [Logger      ] Record log in C:\Users\rahim\.kivy\logs\kivy_21-06-22_121.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.gstreamer" 0.3.2
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.3.1
[INFO   ] [Kivy        ] v2.0.0
[INFO   ] [Kivy        ] Installed at "C:\Users\rahim\PycharmProjects\yt_filter\venv\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "C:\Users\rahim\PycharmProjects\yt_filter\venv\Scripts\python.exe"
[INFO   ] [Logger      ] Purge log fired. Analysing...
[INFO   ] [Logger      ] Purge 51 log files
[INFO   ] [Logger      ] Purge finished!
[INFO   ] [KivyMD      ] 0.104.2, git-bc7d1f5, 2021-06-06 (installed at "C:\Users\rahim\PycharmProjects\yt_filter\venv\lib\site-packages\kivymd\__init__.py")
[INFO   ] [Factory     ] 186 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.6.13596 Compatibility Profile Context 20.10.35.02 27.20.1034.6'>
[INFO   ] [GL          ] OpenGL vendor <b'ATI Technologies Inc.'>
[INFO   ] [GL          ] OpenGL renderer <b'AMD Radeon(TM) Vega 8 Graphics'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [GL          ] NPOT texture support is available
 Traceback (most recent call last):
   File "kivy\properties.pyx", line 861, in kivy.properties.ObservableDict.__getattr__
 KeyError: 'subs_id'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "C:\Users\rahim\PycharmProjects\yt_filter\mian.py", line 90, in <module>
     DemoApp().run()
   File "C:\Users\rahim\PycharmProjects\yt_filter\venv\lib\site-packages\kivy\app.py", line 949, in run
     self._run_prepare()
   File "C:\Users\rahim\PycharmProjects\yt_filter\venv\lib\site-packages\kivy\app.py", line 944, in _run_prepare
     self.dispatch('on_start')
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\rahim\PycharmProjects\yt_filter\mian.py", line 86, in on_start
     self.root.ids.subs_id.add_widget(item)
   File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

Process finished with exit code 1

YouTube 很有用,但算法会毁了你的生活,所以我想出了这个主意...

我的想法是,我只能访问我在应用程序中列出的频道中的视频,摆脱 YouTube 应用程序,告别邪恶的算法。

from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager
from kivymd.uix.list import OneLineListItem

Window.size = (300, 500)

screen_helper = """
ScreenManager:
    HomeScreen:
    SubscriptionScreen:

<HomeScreen>:
    name: 'home'
    BoxLayout:
        orientation: 'vertical'
        MDToolbar:
            title: 'YouTube'
            left_action_items: [["youtube", lambda x: app.navigation_draw()]]
            right_action_items: [["magnify", lambda x: app.search_popup()]]
            MDIconButton:
                icon: "youtube-subscription"
                md_bg_color: app.theme_cls.primary_color
                pos_hint: {'center_x':0.5, 'center_y':0.5}   
                on_press: root.manager.current = "subscriptions"

        MDLabel:
            text: 'hello world'
            halign: 'center'

    MDRectangleFlatButton:
        text:'profile'
        pos_hint: {'center_x':0.5, 'center_y':0.5}   
        on_press: root.manager.current = "subscriptions"

<SubscriptionScreen>:
    name: 'subscriptions'
    BoxLayout:
        orientation: 'vertical'
        MDToolbar:
            title: 'YouTube'
            left_action_items: [["youtube", lambda x: app.navigation_draw()]]
            right_action_items: [["magnify", lambda x: app.search_popup()]]
            MDIconButton:
                icon: "youtube-subscription"
                md_bg_color: app.theme_cls.primary_color
                pos_hint: {'center_x':0.5, 'center_y':0.5}   
                on_press: root.manager.current = "profile"
        ScrollView:
            MDList:
                id: subs_id

                """


class HomeScreen(Screen):
    pass


class SubscriptionScreen(Screen):
    pass


sm = ScreenManager()
sm.add_widget(HomeScreen(name='home'))
subscription_screen = SubscriptionScreen(name='subscriptions')
sm.add_widget(subscription_screen)
sm.current = "home"


class DemoApp(MDApp):

    def build(self):
        self.theme_cls.primary_palette = 'Red'
        screen = Builder.load_string(screen_helper)
        return screen

    def on_start(self):
        print("this is my app", self)
        print("this is the app widget visual", self.root)
        print("this is the dictionnary containing all the id/widgets referenced on THE FIRST LVL of my app",
              self.root.ids)
        print("yes subs_id is not in because each widget can only see the id of the widgets declared on him.")
        print("this is my screenManager", sm)
        print("this is my subscription screen", subscription_screen)


DemoApp().run()