【问题标题】:KIVY ERROR: AttributeError: 'super' object has no attribute '__getattr__'KIVY 错误:AttributeError:“超级”对象没有属性“__getattr__”
【发布时间】:2020-11-20 08:15:18
【问题描述】:

我是这种编程的新手,我正在尝试解决这个错误(python 3,在 PyCharm 上运行):

 Traceback (most recent call last):
   File "kivy\properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
 KeyError: 'lista'
 
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):
   File "C:/...PycharmProjects/RUBRICA/main.py", line 62, in <module>
     application.run()
   File "C:\...AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\app.py", line 829, in run
     root = self.build()
   File "C:/...PycharmProjects/RUBRICA/main.py", line 57, in build
     self.screens[0].ids.lista.data = [{'text='+ str(x)} for x in range(30)]
   File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

##########PY 文件:

from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleboxlayout import RecycleBoxLayout

import os

os.chdir("C:\\Users\\Davide\\PycharmProjects\\RUBRICA")
first_imp = False

class Introduction1Window(Screen):
    def go_to_int2(self):
        application.sm.current = application.screens[2].name

class Introduction2Window(Screen):
    file_chooser: ObjectProperty(None)
    dir = "C:\\Program Files"

    def go_to_main(self):
        application.sm.switch_to(application.screens[0])
    def set_dir(self):
        self.dir = self.file_chooser.path

class NewContact(Screen):
    pass

class ModContact(Screen):
    pass

class MainWindow(Screen):
    lista = ObjectProperty(None)
    def set_data(self):
        self.lista.data = [{'text='+ str(x)} for x in range(30)]
class WindowManager(ScreenManager):
    pass


class Main(App):
    sm = None
    screens = [MainWindow(name="MainWindow"),Introduction1Window(name="Introduction1Window"),Introduction2Window(name="Introduction2Window")]

    def build(self):
        self.sm = WindowManager()
        self.icon = 'intbackgorund.png'
        for screen in self.screens:
            self.sm.add_widget(screen)
        if first_imp:
            self.sm.current = "Introduction1Window"
        else:
            self.sm.current = "MainWindow"
        #self.screens[0].ids.lista.data = [{'text='+ str(x)} for x in range(30)]
        return self.sm.current


application = Main()
application.run()

凡是对程序实际状态无用的部分,以后都会用到本项目中

#######KIVY 文件

<MainWindow>:
    lista: lista
    orientation: "vertical"
    on_enter: root.set_data()
    Label:
        text: "main"
    RecycleView:
        viewclass: 'Button'
        RecycleBoxLayout:
            id: lista
            default_size: None, dp(56)
            default_size_hint: 1, None
            orientation: 'vertical'

如果有人能解释这个问题,我会非常感激。

【问题讨论】:

    标签: python class kivy kivy-language super


    【解决方案1】:

    几个问题:

    • 您的 lista 属性附加到您的 kv 中的错误项目。它应该附加到RecycleView,而不是RecycleBoxLayout
    • 您必须为 RecycleBoxLayout 添加一个 height 属性。

    这是您的kv 的更新版本:

    <MainWindow>:
        lista: lista
        on_enter: root.set_data()
        Label:
            text: "main"
        RecycleView:
            id: lista
            viewclass: 'Button'
            RecycleBoxLayout:
                default_size: None, dp(56)
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'
    
    • data 属性的构造不正确。您应该创建一个字典列表,但您正在创建一个集合列表(每个集合包含一个字符串)。

    这是您的 data 构造的更正版本:

    class MainWindow(Screen):
        lista = ObjectProperty(None)
        def set_data(self, *args):
            self.lista.data = [{'text': str(x)} for x in range(30)]
    

    【讨论】:

      【解决方案2】:

      这并没有完全奏效,事实上我现在得到了这个错误:

      Exception: Invalid instance in App.root
      

      这是更新后的代码:

      from kivy.lang import Builder
      from kivy.properties import ObjectProperty
      from kivy.app import App
      from kivy.uix.boxlayout import BoxLayout
      from kivy.uix.popup import Popup
      from kivy.uix.screenmanager import Screen, ScreenManager
      from kivy.factory import Factory
      from kivy.properties import ObjectProperty
      from kivy.uix.recycleview import RecycleView
      from kivy.uix.recycleboxlayout import RecycleBoxLayout
      
      import os
      
      os.chdir("C:\\Users\\Davide\\PycharmProjects\\RUBRICA")
      first_imp = False
      
      class Introduction1Window(Screen):
      
          def go_to_int2(self):
              application.sm.current = application.screens[2].name
      
      
      class Introduction2Window(Screen):
          file_chooser: ObjectProperty(None)
          dir = "C:\\Program Files"
      
          def go_to_main(self):
              application.sm.switch_to(application.screens[0])
          def set_dir(self):
              self.dir = self.file_chooser.path
      
      #class NewContact(Screen):
          #pass
      
      #class ModContact(Screen):
          #àpass
      
      class MainWindow(Screen):
          lista = ObjectProperty(None)
      
          def set_data(self,*args):
              self.lista.data = [{'text=': str(x)} for x in range(30)]
      
      
      class WindowManager(ScreenManager):
          pass
      
      
      class Main(App):
          sm = None
          screens = [MainWindow(name="MainWindow"),Introduction1Window(name="Introduction1Window"),Introduction2Window(name="Introduction2Window")]
      
          def build(self):
              self.sm = WindowManager()
              for screen in self.screens:
                  self.sm.add_widget(screen)
              if first_imp:
                  self.sm.current = "Introduction1Window"
              else:
                  self.sm.current = "MainWindow"
              return self.sm.current
      
      
      
      application = Main()
      application.run()
      

      基维文件:

      <MainWindow>:
          lista: lista
          on_enter: root.set_data()
          Label:
              text: "main"
          RecycleView:
              id: lista
              viewclass: 'Button'
              RecycleBoxLayout:
                  default_size: None, dp(56)
                  default_size_hint: 1, None
                  size_hint_y: None
                  height: self.minimum_height
                  orientation: 'vertical'
      

      【讨论】:

        猜你喜欢
        • 2020-09-03
        • 1970-01-01
        • 1970-01-01
        • 2022-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-09
        • 1970-01-01
        相关资源
        最近更新 更多