How to solve kivy error: AttributeError: 'super' object has no attribute '__getattr__'
How to solve kivy error: AttributeError: 'super' object has no attribute '__getattr__'
我是 kivy 的新手,我创建了一个使用 2 个微调器的表单,第一个微调器包含一个值列表,当 selected 它将调用 .py 文件中的函数并更改值第二个微调器。但是每当我 select 来自第一个微调器的值时 "AttributeError: 'super' object has no attribute 'getattr'" 被展示。
我已经尝试了很多东西但无法使它工作,请任何尝试都表示赞赏。
我的.py文件:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
#dummy screen
class Blank(Screen):
def on_enter(self):
Clock.schedule_once(self.change_screen)
def change_screen(self, dt):
if True: self.manager.current = 'page1'
else: self.manager.current = 'menu'
class Page1(Screen):
combinations_id = ObjectProperty(None)
def spinner_func(self):
self.ids.combinations.values = ['g','y']
class Menu(Screen):
pass
class Profile(Screen):
pass
class MainMenu(ScreenManager):
pass
kv = Builder.load_file('main.kv')
class SpinApp(App):
def build(self):
self.other = Page1()
return kv
if __name__ == '__main__':
SpinApp().run()
** 这是 .kv 文件:**
MainMenu:
Blank:
Page1:
Menu:
Profile:
<Menu>:
name: 'menu'
Label:
text: 'MENU'
<Profile>:
name: 'profile'
Label:
text: 'profile'
<Page1>:
name: 'page1'
BoxLayout:
combinations_id: combinations_id
orientation: 'vertical'
canvas.before:
Color:
rgb: (253/255, 245/255, 230/255, 1)
Rectangle:
size: self.size
pos: self.pos
Label:
canvas.before:
Color:
rgb: (250/255, 235/255, 210/255, 1)
Rectangle:
size: self.size
pos: self.pos
color: 47/255, 79/255, 79/255, 1
text: 'please fill the form for conveniency'
font_size: 34
italic: True
bold: True
size_hint_y: 0.18
ScrollView:
GridLayout:
size: root.width, root.height
size_hint_y: None
height: 1200
width: self.minimum_width
rows: 3
cols: 1
spacing: 10
RelativeLayout:
Label:
text: 'User Profile'
size_hint: (0.45,None)
height: 100
font_size: 30
bold: True
pos_hint: {'center_x':0.332}
canvas.before:
Color:
rgb: (43/255, 88/255, 108/255, 1)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [15]
#form layout
RelativeLayout:
size_hint_y: None
height: 900
BoxLayout:
orientation: 'vertical'
size_hint_x: None
width: (root.width)*0.8
pos_hint: {'center_x':0.5}
padding: [15,10,10,0]
canvas:
Color:
rgb: (93/255, 138/255, 168/255, 1)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [20,20,20,20]
Label:
id: name
text: '\nName'
size_hint_x: None
size: self.texture_size
pos_hint: {'left': 1}
bold: True
Input_text:
hint_text: 'eg. AbdulAzeez Nuhu'
Input_label:
text: '\nMatric Number'
Input_text:
hint_text: 'eg. se/mat-csc/19/0000'
Input_label:
text: '\nSchool'
My_spinner:
id: schools_id
text: 'Choose your school'
values: ['Science', 'Education', 'Vocational', 'Technical', 'Art and Sos', 'Languages']
on_text: app.other.spinner_func()
Input_label:
text: '\nCombination'
My_spinner:
id: combinations_id
text: 'Choose your Combination'
values: []
on_text: root.comb_func()
Input_label:
text: '\nLevel'
GridLayout:
cols: 7
GridLayout:
cols: 2
CheckBox:
group: 'level'
Label:
text: '100'
Label:
size_hint_x: 0.3
GridLayout:
cols: 2
CheckBox:
group: 'level'
Label:
text: '200'
Label:
size_hint_x: 0.3
GridLayout:
cols: 2
CheckBox:
group: 'level'
Label:
text: '300'
Label:
size_hint_x: 1.5
RelativeLayout:
Round_Button:
text: 'Submit'
size_hint: (0.8, None)
height: 100
pos_hint: {'center_x':0.5, 'top':1}
on_release: app.root.current = 'menu'
<Input_label@Label>:
size_hint_x: None
size: self.texture_size
pos_hint: {'left': 1,}
bold: True
<Input_text@TextInput>:
multiline: False
size_hint_y: 0.8
padding: [10, 10]
background_normal: ''
background_color: 240/255, 248/255, 1, 1
valign: 'bottom'
<My_spinner@Spinner>:
color: 0.5,0.5,0.5,1
size_hint_y: 0.8
text_size: self.size
halign: 'left'
padding: [15,15]
background_normal: ''
background_color: 240/255, 248/255, 1, 1
<SpinnerOption>:
size_hint_y: None
height: 70
color: 1,1,1,1
text_size: self.size
halign: 'left'
padding_x: 15
background_normal: ''
background_color: 112/255, 128/255, 144/255, 1
<Round_Button@Button>:
background_color: (0,0,0,0)
background_normal: ''
canvas.before:
Color:
rgb: (0, 123/255, 167/255, 1) if self.state=='normal' else (0,.7,.7,1)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [20,20,20,20]
输出:
[INFO ] [Logger ] Record log in /storage/emulated/0/kivi/.kivy/logs/kivy_21-05-12_22.txt
[INFO ] [Kivy ] v1.11.1
[INFO ] [Kivy ] Installed at "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/__init__.py"
[INFO ] [Python ] v3.8.3 (default, Jun 15 2020, 02:19:10)
[GCC 9.3.0]
[INFO ] [Python ] Interpreter at "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/bin/python3"
[INFO ] [Factory ] 184 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL ES 2" graphics system
[INFO ] [GL ] Backend used <sdl2>
[INFO ] [GL ] OpenGL version <b'OpenGL ES 2.0'>
[INFO ] [GL ] OpenGL vendor <b'ARM'>
[INFO ] [GL ] OpenGL renderer <b'Mali-400 MP'>
[INFO ] [GL ] OpenGL parsed version: 2, 0
[INFO ] [GL ] Texture max size <4096>
[INFO ] [GL ] Texture max units <8>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [GL ] NPOT texture support is available
[WARNING] [Base ] Unknown <android> provider
[INFO ] [Base ] Start application main loop
[INFO ] [Base ] Leaving application in progress...
Traceback (most recent call last):
File "kivy/properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
KeyError: 'combinations'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/storage/emulated/0/kivi/newfile5.py", line 41, in <module>
SpinApp().run()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/app.py", line 855, in run
runTouchApp()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/base.py", line 504, in runTouchApp
EventLoop.window.mainloop()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/core/window/window_sdl2.py", line 747, in mainloop
self._mainloop()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/core/window/window_sdl2.py", line 479, in _mainloop
EventLoop.idle()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/base.py", line 342, in idle
self.dispatch_input()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/base.py", line 327, in dispatch_input
post_dispatch_input(*pop(0))
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/base.py", line 293, in post_dispatch_input
wid.dispatch('on_touch_up', me)
File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/uix/behaviors/button.py", line 179, in on_touch_up
self.dispatch('on_release')
File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
File "kivy/_event.pyx", line 1138, in kivy._event.EventObservers._dispatch
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/uix/spinner.py", line 196, in <lambda>
item.bind(on_release=lambda option: dp.select(option.text))
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/uix/dropdown.py", line 275, in select
self.dispatch('on_select', data)
File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
File "kivy/_event.pyx", line 1138, in kivy._event.EventObservers._dispatch
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/uix/spinner.py", line 213, in _on_dropdown_select
self.text = data
File "kivy/properties.pyx", line 497, in kivy.properties.Property.__set__
File "kivy/properties.pyx", line 544, in kivy.properties.Property.set
File "kivy/properties.pyx", line 599, in kivy.properties.Property.dispatch
File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
File "kivy/_event.pyx", line 1096, in kivy._event.EventObservers._dispatch
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/lang/builder.py", line 64, in custom_callback
exec(__kvlang__.co_value, idmap)
File "/storage/emulated/0/kivi/main.kv", line 108, in <module>
on_text: app.other.spinner_func()
File "/storage/emulated/0/kivi/newfile5.py", line 21, in spinner_func
self.ids.combinations.values = ['g','y']
File "kivy/properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
您正试图在行中引用不存在的 id
:
self.ids.combinations.values = ['g','y']
我认为您要引用的 id
是 combinations_id
。
尝试将该行更改为:
self.ids.combinations_id.values = ['g', 'y']
此外,在您的 build()
方法中,行:
self.other = Page1()
正在创建 Page1
的新实例,但该实例不是显示在您的 GUI 中的实例,因此可以删除该行。然后。在您的 kv
中,更改行:
on_text: app.other.spinner_func()
至:
on_text: root.spinner_func()
我是 kivy 的新手,我创建了一个使用 2 个微调器的表单,第一个微调器包含一个值列表,当 selected 它将调用 .py 文件中的函数并更改值第二个微调器。但是每当我 select 来自第一个微调器的值时 "AttributeError: 'super' object has no attribute 'getattr'" 被展示。 我已经尝试了很多东西但无法使它工作,请任何尝试都表示赞赏。
我的.py文件:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
#dummy screen
class Blank(Screen):
def on_enter(self):
Clock.schedule_once(self.change_screen)
def change_screen(self, dt):
if True: self.manager.current = 'page1'
else: self.manager.current = 'menu'
class Page1(Screen):
combinations_id = ObjectProperty(None)
def spinner_func(self):
self.ids.combinations.values = ['g','y']
class Menu(Screen):
pass
class Profile(Screen):
pass
class MainMenu(ScreenManager):
pass
kv = Builder.load_file('main.kv')
class SpinApp(App):
def build(self):
self.other = Page1()
return kv
if __name__ == '__main__':
SpinApp().run()
** 这是 .kv 文件:**
MainMenu:
Blank:
Page1:
Menu:
Profile:
<Menu>:
name: 'menu'
Label:
text: 'MENU'
<Profile>:
name: 'profile'
Label:
text: 'profile'
<Page1>:
name: 'page1'
BoxLayout:
combinations_id: combinations_id
orientation: 'vertical'
canvas.before:
Color:
rgb: (253/255, 245/255, 230/255, 1)
Rectangle:
size: self.size
pos: self.pos
Label:
canvas.before:
Color:
rgb: (250/255, 235/255, 210/255, 1)
Rectangle:
size: self.size
pos: self.pos
color: 47/255, 79/255, 79/255, 1
text: 'please fill the form for conveniency'
font_size: 34
italic: True
bold: True
size_hint_y: 0.18
ScrollView:
GridLayout:
size: root.width, root.height
size_hint_y: None
height: 1200
width: self.minimum_width
rows: 3
cols: 1
spacing: 10
RelativeLayout:
Label:
text: 'User Profile'
size_hint: (0.45,None)
height: 100
font_size: 30
bold: True
pos_hint: {'center_x':0.332}
canvas.before:
Color:
rgb: (43/255, 88/255, 108/255, 1)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [15]
#form layout
RelativeLayout:
size_hint_y: None
height: 900
BoxLayout:
orientation: 'vertical'
size_hint_x: None
width: (root.width)*0.8
pos_hint: {'center_x':0.5}
padding: [15,10,10,0]
canvas:
Color:
rgb: (93/255, 138/255, 168/255, 1)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [20,20,20,20]
Label:
id: name
text: '\nName'
size_hint_x: None
size: self.texture_size
pos_hint: {'left': 1}
bold: True
Input_text:
hint_text: 'eg. AbdulAzeez Nuhu'
Input_label:
text: '\nMatric Number'
Input_text:
hint_text: 'eg. se/mat-csc/19/0000'
Input_label:
text: '\nSchool'
My_spinner:
id: schools_id
text: 'Choose your school'
values: ['Science', 'Education', 'Vocational', 'Technical', 'Art and Sos', 'Languages']
on_text: app.other.spinner_func()
Input_label:
text: '\nCombination'
My_spinner:
id: combinations_id
text: 'Choose your Combination'
values: []
on_text: root.comb_func()
Input_label:
text: '\nLevel'
GridLayout:
cols: 7
GridLayout:
cols: 2
CheckBox:
group: 'level'
Label:
text: '100'
Label:
size_hint_x: 0.3
GridLayout:
cols: 2
CheckBox:
group: 'level'
Label:
text: '200'
Label:
size_hint_x: 0.3
GridLayout:
cols: 2
CheckBox:
group: 'level'
Label:
text: '300'
Label:
size_hint_x: 1.5
RelativeLayout:
Round_Button:
text: 'Submit'
size_hint: (0.8, None)
height: 100
pos_hint: {'center_x':0.5, 'top':1}
on_release: app.root.current = 'menu'
<Input_label@Label>:
size_hint_x: None
size: self.texture_size
pos_hint: {'left': 1,}
bold: True
<Input_text@TextInput>:
multiline: False
size_hint_y: 0.8
padding: [10, 10]
background_normal: ''
background_color: 240/255, 248/255, 1, 1
valign: 'bottom'
<My_spinner@Spinner>:
color: 0.5,0.5,0.5,1
size_hint_y: 0.8
text_size: self.size
halign: 'left'
padding: [15,15]
background_normal: ''
background_color: 240/255, 248/255, 1, 1
<SpinnerOption>:
size_hint_y: None
height: 70
color: 1,1,1,1
text_size: self.size
halign: 'left'
padding_x: 15
background_normal: ''
background_color: 112/255, 128/255, 144/255, 1
<Round_Button@Button>:
background_color: (0,0,0,0)
background_normal: ''
canvas.before:
Color:
rgb: (0, 123/255, 167/255, 1) if self.state=='normal' else (0,.7,.7,1)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [20,20,20,20]
输出:
[INFO ] [Logger ] Record log in /storage/emulated/0/kivi/.kivy/logs/kivy_21-05-12_22.txt
[INFO ] [Kivy ] v1.11.1
[INFO ] [Kivy ] Installed at "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/__init__.py"
[INFO ] [Python ] v3.8.3 (default, Jun 15 2020, 02:19:10)
[GCC 9.3.0]
[INFO ] [Python ] Interpreter at "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/bin/python3"
[INFO ] [Factory ] 184 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL ES 2" graphics system
[INFO ] [GL ] Backend used <sdl2>
[INFO ] [GL ] OpenGL version <b'OpenGL ES 2.0'>
[INFO ] [GL ] OpenGL vendor <b'ARM'>
[INFO ] [GL ] OpenGL renderer <b'Mali-400 MP'>
[INFO ] [GL ] OpenGL parsed version: 2, 0
[INFO ] [GL ] Texture max size <4096>
[INFO ] [GL ] Texture max units <8>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [GL ] NPOT texture support is available
[WARNING] [Base ] Unknown <android> provider
[INFO ] [Base ] Start application main loop
[INFO ] [Base ] Leaving application in progress...
Traceback (most recent call last):
File "kivy/properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
KeyError: 'combinations'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/storage/emulated/0/kivi/newfile5.py", line 41, in <module>
SpinApp().run()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/app.py", line 855, in run
runTouchApp()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/base.py", line 504, in runTouchApp
EventLoop.window.mainloop()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/core/window/window_sdl2.py", line 747, in mainloop
self._mainloop()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/core/window/window_sdl2.py", line 479, in _mainloop
EventLoop.idle()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/base.py", line 342, in idle
self.dispatch_input()
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/base.py", line 327, in dispatch_input
post_dispatch_input(*pop(0))
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/base.py", line 293, in post_dispatch_input
wid.dispatch('on_touch_up', me)
File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/uix/behaviors/button.py", line 179, in on_touch_up
self.dispatch('on_release')
File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
File "kivy/_event.pyx", line 1138, in kivy._event.EventObservers._dispatch
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/uix/spinner.py", line 196, in <lambda>
item.bind(on_release=lambda option: dp.select(option.text))
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/uix/dropdown.py", line 275, in select
self.dispatch('on_select', data)
File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
File "kivy/_event.pyx", line 1138, in kivy._event.EventObservers._dispatch
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/uix/spinner.py", line 213, in _on_dropdown_select
self.text = data
File "kivy/properties.pyx", line 497, in kivy.properties.Property.__set__
File "kivy/properties.pyx", line 544, in kivy.properties.Property.set
File "kivy/properties.pyx", line 599, in kivy.properties.Property.dispatch
File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
File "kivy/_event.pyx", line 1096, in kivy._event.EventObservers._dispatch
File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/kivy/lang/builder.py", line 64, in custom_callback
exec(__kvlang__.co_value, idmap)
File "/storage/emulated/0/kivi/main.kv", line 108, in <module>
on_text: app.other.spinner_func()
File "/storage/emulated/0/kivi/newfile5.py", line 21, in spinner_func
self.ids.combinations.values = ['g','y']
File "kivy/properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
您正试图在行中引用不存在的 id
:
self.ids.combinations.values = ['g','y']
我认为您要引用的 id
是 combinations_id
。
尝试将该行更改为:
self.ids.combinations_id.values = ['g', 'y']
此外,在您的 build()
方法中,行:
self.other = Page1()
正在创建 Page1
的新实例,但该实例不是显示在您的 GUI 中的实例,因此可以删除该行。然后。在您的 kv
中,更改行:
on_text: app.other.spinner_func()
至:
on_text: root.spinner_func()