Kivy throws this error while populating json data in kivy app using ids: 'super' object has no attribute '__

huangapple go评论47阅读模式
英文:

Kivy throws this error while populating json data in kivy app using ids: 'super' object has no attribute '__

问题

我正在编写一个Kivy程序,将存储的JSON中的新闻文章填充到Kivy应用程序中。

在Kivy文件中,HomePage具有带有ID的盒式布局,我尝试使用Python文件使用ID填充相同的内容。但是ID不起作用。我尝试打印ID字典,结果是空白的。

代码如下:

import json
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen
from components.news_scroll import NewsScroll

Builder.load_file('main.kv')

class HomePage(Screen):
    def on_enter(self):
        self.list_news()

    def list_news(self):
        with open('news2023-07-15.json') as user_file:
            parsed_json = json.load(user_file)
            for result in parsed_json['results']:
                self.ids._news.add_widget(NewsScroll(
                    news_title=result['title'],
                    news_content=result['content']
                ))

class RootWidget(ScreenManager):
    pass

class MainApp(MDApp):
    def build(self):
        return RootWidget()

MainApp().run()

Kivy文件如下:

<RootWidget>:
    HomePage:

<HomePage>:
    BoxLayout:
        orientation: 'vertical'
        id: _news

我得到了这个错误:

Traceback (most recent call last):
  File "kivy/properties.pyx", line 961, in kivy.properties.ObservableDict.__getattr__
KeyError: '_news'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/main.py", line 37, in <module>
    MainApp().run()
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/app.py", line 955, in run
    self._run_prepare()
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/app.py", line 925, in _run_prepare
    root = self.build()
           ^^^^^^^^^^^^
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/main.py", line 34, in build
    return RootWidget()
           ^^^^^^^^^^^^
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/screenmanager.py", line 973, in __init__
    super(ScreenManager, self).__init__(**kwargs)
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/floatlayout.py", line 65, in __init__
    super(FloatLayout, self).__init__(**kwargs)
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/layout.py", line 76, in __init__
    super(Layout, self).__init__(**kwargs)
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/widget.py", line 366, in __init__
    self.apply_class_lang_rules(
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/widget.py", line 470, in apply_class_lang_rules
    Builder.apply(
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/lang/builder.py", line 540, in apply
    self._apply_rule(
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/lang/builder.py", line 659, in _apply_rule
    widget.add_widget(child)
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/screenmanager.py", line 1001, in add_widget
    self.current = widget.name
    ^^^^^^^^^^^^
  File "kivy/properties.pyx", line 520, in kivy.properties.Property.__set__
  File "kivy/properties.pyx", line 567, in kivy.properties.Property.set
  File "kivy/properties.pyx", line 606, in kivy.properties.Property._dispatch
  File "kivy/_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
  File "kivy/_event.pyx", line 1213, in kivy._event.EventObservers._dispatch
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/screenmanager.py", line 1069, in on_current
    screen.dispatch('on_enter')
  File "kivy/_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/main.py", line 15, in on_enter
    self.list_news()
  File "/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/main.py", line 22, in list_news
    self.ids._news.add_widget(NewsScroll(
  ^^^^^^^^^^^^^^
  File "kivy/properties.pyx", line 964, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'. Did you mean: '__setattr__'?

我尝试了多种方法,但都失败了。如果您有解决方案,请帮助我。我尝试了__init__函数,但错误仍然相同。

英文:

I am writing a kivy program to populate news articles from stored json to kivy app.
In kivy file: HomePage has box layout with id and I am trying to populate the same with python file using ids. ids is not working. I tried printing ids dictionary and the same, is printed blank.
Code is as below :

import json

from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen

from components.news_scroll import NewsScroll

Builder.load_file(&#39;main.kv&#39;)


class HomePage(Screen):
    def on_enter(self):
        self.list_news()

    def list_news(self):

        with open(&#39;news2023-07-15.json&#39;) as user_file:
            parsed_json = json.load(user_file)
            for result in parsed_json[&#39;results&#39;]:
                self.ids._news.add_widget(NewsScroll(
                    news_title=result[&#39;title&#39;],
                    news_content=result[&#39;content&#39;]
                ))


class RootWidget(ScreenManager):
    pass


class MainApp(MDApp):
    def build(self):
        return RootWidget()


MainApp().run()

Kivy file is as below:

#: import NewsScroll components.news_scroll.NewsScroll
&lt;RootWidget&gt;:
    HomePage:

&lt;HomePage&gt;:
    BoxLayout:
        orientation: &#39;vertical&#39;
        id: _news

I am getting this error:

 Traceback (most recent call last):
   File &quot;kivy/properties.pyx&quot;, line 961, in kivy.properties.ObservableDict.__getattr__
 KeyError: &#39;_news&#39;
 
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/main.py&quot;, line 37, in &lt;module&gt;
     MainApp().run()
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/app.py&quot;, line 955, in run
     self._run_prepare()
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/app.py&quot;, line 925, in _run_prepare
     root = self.build()
            ^^^^^^^^^^^^
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/main.py&quot;, line 34, in build
     return RootWidget()
            ^^^^^^^^^^^^
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/screenmanager.py&quot;, line 973, in __init__
     super(ScreenManager, self).__init__(**kwargs)
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/floatlayout.py&quot;, line 65, in __init__
     super(FloatLayout, self).__init__(**kwargs)
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/layout.py&quot;, line 76, in __init__
     super(Layout, self).__init__(**kwargs)
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/widget.py&quot;, line 366, in __init__
     self.apply_class_lang_rules(
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/widget.py&quot;, line 470, in apply_class_lang_rules
     Builder.apply(
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/lang/builder.py&quot;, line 540, in apply
     self._apply_rule(
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/lang/builder.py&quot;, line 659, in _apply_rule
     widget.add_widget(child)
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/screenmanager.py&quot;, line 1001, in add_widget
     self.current = widget.name
     ^^^^^^^^^^^^
   File &quot;kivy/properties.pyx&quot;, line 520, in kivy.properties.Property.__set__
   File &quot;kivy/properties.pyx&quot;, line 567, in kivy.properties.Property.set
   File &quot;kivy/properties.pyx&quot;, line 606, in kivy.properties.Property._dispatch
   File &quot;kivy/_event.pyx&quot;, line 1307, in kivy._event.EventObservers.dispatch
   File &quot;kivy/_event.pyx&quot;, line 1213, in kivy._event.EventObservers._dispatch
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/kivy/uix/screenmanager.py&quot;, line 1069, in on_current
     screen.dispatch(&#39;on_enter&#39;)
   File &quot;kivy/_event.pyx&quot;, line 731, in kivy._event.EventDispatcher.dispatch
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/main.py&quot;, line 15, in on_enter
     self.list_news()
   File &quot;/Users/pradnyanandmanoharjadhav/PycharmProjects/pythonProject/main.py&quot;, line 22, in list_news
     self.ids._news.add_widget(NewsScroll(
     ^^^^^^^^^^^^^^
   File &quot;kivy/properties.pyx&quot;, line 964, in kivy.properties.ObservableDict.__getattr__
 AttributeError: &#39;super&#39; object has no attribute &#39;__getattr__&#39;. Did you mean: &#39;__setattr__&#39;?

I tried multiple things but I failed at everything.
If you have some solution, kindly assist.

I tried init function. Error stays the same.

答案1

得分: 0

问题在于在触发on_enter()方法时,HomePageids尚未填充。一个解决方法是稍微延迟调用list_news(),直到ids字典完全填充。您可以像这样使用Clock.schedule_once()

def on_enter(self):
    # self.list_news()
    Clock.schedule_once(self.list_news)

这需要对list_news()的签名进行一个小的改动:

def list_news(self, *args):

这考虑到了Clock.schedule_once()添加到调用中的dt参数。

英文:

The problem is that the ids of the HomePage are not yet filled in when the on_enter() method is triggered. A fix is to just delay the call to list_news() slightly, until the ids dictionary is complete. You can do this by using Clock.schedule_once() like this:

def on_enter(self):
    # self.list_news()
    Clock.schedule_once(self.list_news)

This requires a small change in the list_news() signature:

def list_news(self, *args):

This accounts for the dt argument that Clock.schedule_once() adds to the call.

huangapple
  • 本文由 发表于 2023年7月17日 09:31:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定