Python Kivy错误:AttributeError:&x27;超级';对象没有属性'__getattr';关于self.root.ids
有人能告诉我为什么按钮没有任何作用,为什么self.root.ids.container.add\u小部件( OneLineListItem(text=f“单行项{i}”)发出错误 “AttributeError:'超级'对象没有属性'getattr'” 我在基维真的没有那么先进,如果你们能帮我,我真的很感激, 提前非常感谢你 以下是全部错误:Python Kivy错误:AttributeError:&x27;超级';对象没有属性'__getattr';关于self.root.ids,python,kivy,kivymd,Python,Kivy,Kivymd,有人能告诉我为什么按钮没有任何作用,为什么self.root.ids.container.add\u小部件( OneLineListItem(text=f“单行项{i}”)发出错误 “AttributeError:'超级'对象没有属性'getattr'” 我在基维真的没有那么先进,如果你们能帮我,我真的很感激, 提前非常感谢你 以下是全部错误: Traceback (most recent call last): File "kivy\properties.pyx",
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__'
回溯(最近一次呼叫最后一次):
文件“kivy\properties.pyx”,第861行,位于kivy.properties.observedict.\uu getattr__
KeyError:“容器”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“c:\Users\Acer\Desktop\Proj\Python\Laboratory\help.py”,第65行,在
Test().run()
文件“C:\Python\Python39\lib\site packages\kivy\app.py”,第950行,正在运行
runTouchApp()
runTouchApp中的文件“C:\Python\Python39\lib\site packages\kivy\base.py”,第582行
EventLoop.mainloop()
mainloop中第347行的文件“C:\Python\Python39\lib\site packages\kivy\base.py”
self.idle()
文件“C:\Python\Python39\lib\site packages\kivy\base.py”,第391行,处于空闲状态
self.dispatch_input()
文件“C:\Python\Python39\lib\site packages\kivy\base.py”,第342行,在dispatch\u输入中
发送后输入(*pop(0))
文件“C:\Python\Python39\lib\site packages\kivy\base.py”,第308行,在post\u dispatch\u输入中
wid.dispatch('我在润色时)
文件“kivy\\ u event.pyx”,第709行,在kivy.\u event.EventDispatcher.dispatch中
文件“C:\Python\Python39\lib\site packages\kivymd\uix\behaviors\ripple\u behavior.py”,第296行,在on\u touch\u中
在触摸时返回super()(触摸)
文件“C:\Python\Python39\lib\site packages\kivymd\uix\button.py”,第982行,在on\u touch\u中
在触摸时返回super()(触摸)
文件“C:\Python\Python39\lib\site packages\kivy\uix\behaviors\button.py”,第179行,在on\u touch\u中
自我分派(“发布时”)
文件“kivy\\ u event.pyx”,第705行,在kivy.\u event.EventDispatcher.dispatch中
文件“kivy\\u event.pyx”,第1248行,在kivy.\u event.eventobserver.dispatch中
文件“kivy\\ u event.pyx”,第1172行,位于kivy.\u event.eventobserver.\u调度
文件“c:\Users\Acer\Desktop\Proj\Python\Laboratory\help.py”,第51行,在
on_release=lambda x:self.add()
文件“c:\Users\Acer\Desktop\Proj\Python\Laboratory\help.py”,第58行,添加
self.root.ids.container.add_小部件(OneLineListItem(text=f“单行项”))
文件“kivy\properties.pyx”,第864行,位于kivy.properties.observedict.\uu getattr__
AttributeError:“super”对象没有属性“\uuu getattr\uuuu”
首先让我们看看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()
它继承自App
和FpsMonitoring
类,您可以在(此处)中阅读有关多重继承的更多信息[https://www.geeksforgeeks.org/multiple-inheritance-in-python/]
让我们深入了解这两个类:
FPS监控
:
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行找到,实现在),并且生成器类型没有实现getattr方法!因此,如果您想使用它,您需要在self.root中找到另一种访问ids
的方法,请阅读。
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)