Kivy AttributeError: 'super' object has no attribute '__getattr__' (Tried all previous solutions)
Kivy AttributeError: 'super' object has no attribute '__getattr__' (Tried all previous solutions)
这是 Python 中最烦人的错误之一,根据问题有很多解决方案
我的文件
Main.py
# imports
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
# Defining Screen, ScreenManager And App Classes:
class MainScreen(Screen):
pass
class ShortCuts(Screen):
pass
class WindowManager(ScreenManager):
pass
# Kivy Lang Builder Packed As A Variable
BLD = Builder.load_file("KV_FILE.kv")
class OnlineClassMacros(App):
# Initializing The App
def __init__(self, **kwargs):
super(OnlineClassMacros, self).__init__(**kwargs)
# Defining The App By Referencing The Packed Kivy.Lang.Builder Variable Created At The Start Of The .py File
def build(self):
return BLD
@staticmethod
def Get():
query = BLD.ids.SearchEngine.text
print(query)
# ToDo: Monetize The >>Online Class Macros<< Application By Using KivMob
# MainLooping The Application Using [if __name__ == "__main__":]
if __name__ == "__main__":
OnlineClassMacros().run()
KV_FILE.kv
# Importing Our Function.py As A Python Module Into Kivy-Lang\
#######################################################
#:import redirect Function.redirect
#######################################################
# Pre-Styling Widgets
<Label>
halign: 'center'
valign: 'center'
background_color: '0', '.4', '.3', '.4'
<Button>
halign: 'center'
valign: 'center'
background_color: "0", ".1", ".1", ".8"
# Configuring The Screens And The Screen Manager
WindowManager:
MainScreen:
ShortCuts:
# Building All The Classes In Our "Main.py" File
<MainScreen>:
name: "MainScreen"
canvas.before:
Rectangle:
size: self.size
pos: self.pos
source: "Images/background 2.png"
BoxLayout:
orientation: "horizontal"
BoxLayout:
size_hint: .2,1
orientation: "vertical"
Button:
text: "Google\nDocs"
on_release: redirect("https://www.docs.google.com")
Button:
text: "Google\nSlides"
on_release:
redirect("https://slides.google.com/u/0/h")
Button:
text: "Gmail"
on_release:
redirect("https://www.gmail.com")
Button:
text: "Google\nClassroom"
on_release:
redirect("https://classroom.google.com/u/0/")
BoxLayout:
size_hint: .6,1
BoxLayout:
orientation: "vertical"
TextInput:
size_hint: 1, .2
multiline: True
hint_text: "Search here"
background_color: 1,1,1,.78
id: SearchEngine
GridLayout:
cols: 3
Button:
text: "Google Scholar\nSearch"
Button:
text: "Wikipedia\nSearch"
on_release:
app.Get()
Button:
text: "Google\nSearch"
Button:
text: "RefSeek\nSearch"
Button:
text: "Microsoft Academic\nSearch"
Button:
text: "Hyper Search"
background_color: 1,0,0,.99
BoxLayout:
size_hint: .2,1
orientation: "vertical"
Button:
text: "Google\nSpreadSheets"
on_release:
redirect("https://classroom.google.com/u/0/h")
Button:
text: "MicroSoft\nTeams"
on_release:
redirect("https://classroom.google.com/u/0/h")
Button:
text: "OutLook"
on_release:
redirect("https://classroom.google.com/u/0/h")
<ShortCuts>:
canvas.before:
Rectangle:
pos: self.pos
size: self.pos
source: 'Images/ Background 3.png'
错误:
C:\Users\mayan\AppData\Local\Programs\Python\Python39\python.exe "C:/Online Class Macros ;)/Main.py"
[INFO ] [Logger ] Record log in C:\Users\mayan\.kivy\logs\kivy_21-06-13_7.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\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\__init__.py"
[INFO ] [Python ] v3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)]
[INFO ] [Python ] Interpreter at "C:\Users\mayan\AppData\Local\Programs\Python\Python39\python.exe"
[INFO ] [Factory ] 186 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[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.0.0 - Build 10.18.10.5059'>
[INFO ] [GL ] OpenGL vendor <b'Intel'>
[INFO ] [GL ] OpenGL renderer <b'Intel(R) HD Graphics'>
[INFO ] [GL ] OpenGL parsed version: 4, 0
[INFO ] [GL ] Shading version <b'4.00 - Build 10.18.10.5059'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <16>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [GL ] NPOT texture support is available
[INFO ] [GL ] Unpack subimage support is available
[INFO ] [Text ] Provider: sdl2
[INFO ] [Base ] Start application main loop
[INFO ] [Base ] Leaving application in progress...
Traceback (most recent call last):
File "kivy\properties.pyx", line 861, in kivy.properties.ObservableDict.__getattr__
KeyError: 'SearchEngine'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Online Class Macros ;)\Main.py", line 46, in <module>
OnlineClassMacros().run()
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\app.py", line 950, in run
runTouchApp()
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 582, in runTouchApp
EventLoop.mainloop()
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 347, in mainloop
self.idle()
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 391, in idle
self.dispatch_input()
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 342, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\mayan\AppData\Local\Programs\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:\Users\mayan\AppData\Local\Programs\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 1132, in kivy._event.EventObservers._dispatch
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\lang\builder.py", line 57, in custom_callback
exec(__kvlang__.co_value, idmap)
File "C:\Online Class Macros ;)\KV_FILE.kv", line 70, in <module>
app.Get()
File "C:\Online Class Macros ;)\Main.py", line 36, in Get
query = BLD.ids.SearchEngine.text
File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
Process finished with exit code 1
这个错误发生在我身上,我解决了它,但正如我所说,答案取决于问题。
如果没有人删除我的问题说它是重复的,我真的很感激。
神秘的错误消息没有多大帮助,但堆栈跟踪显示错误发生在以下行:
query = BLD.ids.SearchEngine.text
此行失败的原因是您试图访问 BLD
对象中不存在的 id
(SearchEngine
)。 BLD
对象是 kv
的根,即 WindowManager
,但 SearchEngine
id
是在 <MainScreen>:
规则中定义的。所以解决方法是通过正确的对象访问该 id:
def Get():
app = App.get_running_app() # get a refrence to the running App
main_screen = app.root.get_screen('MainScreen') # get a reference to the MainScreen
query = main_screen.ids.SearchEngine.text # now use the id
print(query)
请注意 documentation 表示:
When the kv file is processed, weakrefs to all the widgets tagged with
ids are added to the root widget’s ids dictionary.
该文档不正确。 ids
实际上添加到包含 id
的规则的基础对象中,而不是添加到 kv
文件的根小部件中。
这是 Python 中最烦人的错误之一,根据问题有很多解决方案
我的文件
Main.py
# imports
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
# Defining Screen, ScreenManager And App Classes:
class MainScreen(Screen):
pass
class ShortCuts(Screen):
pass
class WindowManager(ScreenManager):
pass
# Kivy Lang Builder Packed As A Variable
BLD = Builder.load_file("KV_FILE.kv")
class OnlineClassMacros(App):
# Initializing The App
def __init__(self, **kwargs):
super(OnlineClassMacros, self).__init__(**kwargs)
# Defining The App By Referencing The Packed Kivy.Lang.Builder Variable Created At The Start Of The .py File
def build(self):
return BLD
@staticmethod
def Get():
query = BLD.ids.SearchEngine.text
print(query)
# ToDo: Monetize The >>Online Class Macros<< Application By Using KivMob
# MainLooping The Application Using [if __name__ == "__main__":]
if __name__ == "__main__":
OnlineClassMacros().run()
KV_FILE.kv
# Importing Our Function.py As A Python Module Into Kivy-Lang\
#######################################################
#:import redirect Function.redirect
#######################################################
# Pre-Styling Widgets
<Label>
halign: 'center'
valign: 'center'
background_color: '0', '.4', '.3', '.4'
<Button>
halign: 'center'
valign: 'center'
background_color: "0", ".1", ".1", ".8"
# Configuring The Screens And The Screen Manager
WindowManager:
MainScreen:
ShortCuts:
# Building All The Classes In Our "Main.py" File
<MainScreen>:
name: "MainScreen"
canvas.before:
Rectangle:
size: self.size
pos: self.pos
source: "Images/background 2.png"
BoxLayout:
orientation: "horizontal"
BoxLayout:
size_hint: .2,1
orientation: "vertical"
Button:
text: "Google\nDocs"
on_release: redirect("https://www.docs.google.com")
Button:
text: "Google\nSlides"
on_release:
redirect("https://slides.google.com/u/0/h")
Button:
text: "Gmail"
on_release:
redirect("https://www.gmail.com")
Button:
text: "Google\nClassroom"
on_release:
redirect("https://classroom.google.com/u/0/")
BoxLayout:
size_hint: .6,1
BoxLayout:
orientation: "vertical"
TextInput:
size_hint: 1, .2
multiline: True
hint_text: "Search here"
background_color: 1,1,1,.78
id: SearchEngine
GridLayout:
cols: 3
Button:
text: "Google Scholar\nSearch"
Button:
text: "Wikipedia\nSearch"
on_release:
app.Get()
Button:
text: "Google\nSearch"
Button:
text: "RefSeek\nSearch"
Button:
text: "Microsoft Academic\nSearch"
Button:
text: "Hyper Search"
background_color: 1,0,0,.99
BoxLayout:
size_hint: .2,1
orientation: "vertical"
Button:
text: "Google\nSpreadSheets"
on_release:
redirect("https://classroom.google.com/u/0/h")
Button:
text: "MicroSoft\nTeams"
on_release:
redirect("https://classroom.google.com/u/0/h")
Button:
text: "OutLook"
on_release:
redirect("https://classroom.google.com/u/0/h")
<ShortCuts>:
canvas.before:
Rectangle:
pos: self.pos
size: self.pos
source: 'Images/ Background 3.png'
错误:
C:\Users\mayan\AppData\Local\Programs\Python\Python39\python.exe "C:/Online Class Macros ;)/Main.py"
[INFO ] [Logger ] Record log in C:\Users\mayan\.kivy\logs\kivy_21-06-13_7.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\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\__init__.py"
[INFO ] [Python ] v3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)]
[INFO ] [Python ] Interpreter at "C:\Users\mayan\AppData\Local\Programs\Python\Python39\python.exe"
[INFO ] [Factory ] 186 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[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.0.0 - Build 10.18.10.5059'>
[INFO ] [GL ] OpenGL vendor <b'Intel'>
[INFO ] [GL ] OpenGL renderer <b'Intel(R) HD Graphics'>
[INFO ] [GL ] OpenGL parsed version: 4, 0
[INFO ] [GL ] Shading version <b'4.00 - Build 10.18.10.5059'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <16>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [GL ] NPOT texture support is available
[INFO ] [GL ] Unpack subimage support is available
[INFO ] [Text ] Provider: sdl2
[INFO ] [Base ] Start application main loop
[INFO ] [Base ] Leaving application in progress...
Traceback (most recent call last):
File "kivy\properties.pyx", line 861, in kivy.properties.ObservableDict.__getattr__
KeyError: 'SearchEngine'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Online Class Macros ;)\Main.py", line 46, in <module>
OnlineClassMacros().run()
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\app.py", line 950, in run
runTouchApp()
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 582, in runTouchApp
EventLoop.mainloop()
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 347, in mainloop
self.idle()
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 391, in idle
self.dispatch_input()
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 342, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\mayan\AppData\Local\Programs\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:\Users\mayan\AppData\Local\Programs\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 1132, in kivy._event.EventObservers._dispatch
File "C:\Users\mayan\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\lang\builder.py", line 57, in custom_callback
exec(__kvlang__.co_value, idmap)
File "C:\Online Class Macros ;)\KV_FILE.kv", line 70, in <module>
app.Get()
File "C:\Online Class Macros ;)\Main.py", line 36, in Get
query = BLD.ids.SearchEngine.text
File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
Process finished with exit code 1
这个错误发生在我身上,我解决了它,但正如我所说,答案取决于问题。 如果没有人删除我的问题说它是重复的,我真的很感激。
神秘的错误消息没有多大帮助,但堆栈跟踪显示错误发生在以下行:
query = BLD.ids.SearchEngine.text
此行失败的原因是您试图访问 BLD
对象中不存在的 id
(SearchEngine
)。 BLD
对象是 kv
的根,即 WindowManager
,但 SearchEngine
id
是在 <MainScreen>:
规则中定义的。所以解决方法是通过正确的对象访问该 id:
def Get():
app = App.get_running_app() # get a refrence to the running App
main_screen = app.root.get_screen('MainScreen') # get a reference to the MainScreen
query = main_screen.ids.SearchEngine.text # now use the id
print(query)
请注意 documentation 表示:
When the kv file is processed, weakrefs to all the widgets tagged with ids are added to the root widget’s ids dictionary.
该文档不正确。 ids
实际上添加到包含 id
的规则的基础对象中,而不是添加到 kv
文件的根小部件中。