【问题标题】:Kivy error: AttributeError: 'super' object has no attribute '__getattr__' on self.root.idsKivy 错误:AttributeError:'super' 对象在 self.root.ids 上没有属性 '__getattr__'
【发布时间】:2021-04-01 22:00:42
【问题描述】:

谁能帮我为什么按钮不做任何事情以及为什么 self.root.ids.container.add_widget( OneLineListItem(text=f"Single-line item {i}") 报错,

"AttributeError: 'super' 对象没有属性 'getattr'"

from kivy.lang import Builder
from logging import root
from kivymd.uix.screen import Screen
from kivymd.app import MDApp
from kivymd.uix.list import OneLineListItem
from kivymd.uix.boxlayout import BoxLayout
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton
from KVFile import KVee




class Entry(BoxLayout):

    pass

class Test(MDApp):

    bx = BoxLayout()
    
    def build(self):
        
        kvv =  Builder.load_string(KVee)
       
        
        self.bx.add_widget(kvv)

       


        return self.bx
        
    def testFunc(self):

        print("meow!")

    def listItems(self):

        self.dialog = MDDialog(
                title="Task Entry",
                type="custom",
                content_cls=Entry(),
                buttons=[
                    MDFlatButton(
                        text="CANCEL", text_color=self.theme_cls.primary_color,
                        #on_release=self.dialog.
                    ),
                    MDFlatButton(
                        text="OK", text_color=self.theme_cls.primary_color,
                        on_release=lambda x:self.add()
                    ),
                ],
            )
        self.dialog.open()
        
    def add(self):
        self.root.ids.container.add_widget(OneLineListItem(text=f"Single-line item "))
        
    

if __name__ == "__main__":

    Test().run()

我在kivy方面真的没有那么先进,如果你们能帮助我,我真的很感激, 提前非常感谢您!

这是完整的错误:

Traceback (most recent call last):
   File "kivy\properties.pyx", line 861, in kivy.properties.ObservableDict.__getattr__
 KeyError: 'container'
 
 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "c:\Users\Acer\Desktop\Proj\Python\Laboratory\help.py", line 65, in <module>
     Test().run()
   File "C:\Python\Python39\lib\site-packages\kivy\app.py", line 950, in run
     runTouchApp()
   File "C:\Python\Python39\lib\site-packages\kivy\base.py", line 582, in runTouchApp
     EventLoop.mainloop()
   File "C:\Python\Python39\lib\site-packages\kivy\base.py", line 347, in mainloop
     self.idle()
   File "C:\Python\Python39\lib\site-packages\kivy\base.py", line 391, in idle
     self.dispatch_input()
   File "C:\Python\Python39\lib\site-packages\kivy\base.py", line 342, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Python\Python39\lib\site-packages\kivy\base.py", line 308, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "C:\Python\Python39\lib\site-packages\kivymd\uix\behaviors\ripple_behavior.py", line 296, in on_touch_up
     return super().on_touch_up(touch)
   File "C:\Python\Python39\lib\site-packages\kivymd\uix\button.py", line 982, in on_touch_up
     return super().on_touch_up(touch)
   File "C:\Python\Python39\lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
     self.dispatch('on_release')
   File "kivy\_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
   File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx", line 1172, in kivy._event.EventObservers._dispatch
   File "c:\Users\Acer\Desktop\Proj\Python\Laboratory\help.py", line 51, in <lambda>
     on_release=lambda x:self.add()
   File "c:\Users\Acer\Desktop\Proj\Python\Laboratory\help.py", line 58, in add
     self.root.ids.container.add_widget(OneLineListItem(text=f"Single-line item "))
   File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

【问题讨论】:

标签: python kivy kivymd


【解决方案1】:

首先让我们看看MDApp类:

class MDApp(App, FpsMonitoring):
    theme_cls = ObjectProperty()
    """
    Instance of :class:`~ThemeManager` class.
    .. Warning:: The :attr:`~theme_cls` attribute is already available
        in a class that is inherited from the :class:`~MDApp` class.
        The following code will result in an error!
    .. code-block:: python
        class MainApp(MDApp):
            theme_cls = ThemeManager()
            theme_cls.primary_palette = "Teal"
    .. Note:: Correctly do as shown below!
    .. code-block:: python
        class MainApp(MDApp):
            def build(self):
                self.theme_cls.primary_palette = "Teal"
    :attr:`theme_cls` is an :class:`~kivy.properties.ObjectProperty`.
    """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.theme_cls = ThemeManager()

它继承自 AppFpsMonitoring 类,您可以在(此处)[https://www.geeksforgeeks.org/multiple-inheritance-in-python/] 中阅读有关多重继承的更多信息

让我们深入了解这两个类: FpsMonitoring:

class FpsMonitoring:
    """Adds a monitor to display the current FPS in the toolbar."""

    def fps_monitor_start(self):
        from kivy.core.window import Window

        from kivymd.utils.fpsmonitor import FpsMonitor

        monitor = FpsMonitor()
        monitor.start()
        Window.add_widget(monitor)

并且,App 的实现可以在 -https://github.com/kivy/kivy/blob/master/kivy/app.py 找到(文档太长,无法在此处粘贴...)

root var 实际上是来自Builder 类型(可以在上面链接的第696 行找到,以及在https://github.com/kivy/kivy/blob/master/kivy/lang/builder.py 的实现),并且Builder 类型没有实现getattr 方法!所以,如果你想使用它,你需要在self.root找到另一种访问ids的方式。

【讨论】:

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