微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何解决 kivy 错误:AttributeError: 'super' object has no attribute '__getattr__'

如何解决如何解决 kivy 错误:AttributeError: 'super' object has no attribute '__getattr__'

我是 kivy 的新手,我创建了一个使用 2 个微调器的表单,第一个微调器包含一个值列表,选择后它将调用 .py 文件中的函数并更改第二个微调器的值。但是每当我从第一个微调器中选择一个值时,就会显示 "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,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,0]
                        canvas:
                            Color:
                                rgb: (93/255,138/255,168/255,1)
                            RoundedRectangle:
                                size: self.size
                                pos: self.pos
                                radius: [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,1
    size_hint_y: 0.8
    text_size: self.size
    halign: 'left'
    padding: [15,15]
    background_normal: ''
    background_color: 240/255,1

<SpinnerOption>:
    size_hint_y: None
    height: 70
    color: 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)
    background_normal: ''
    canvas.before:
        Color:
            rgb: (0,123/255,167/255,1) if self.state=='normal' else (0,.7,1)
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: [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 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 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']

我相信您尝试引用的 idcombinations_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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。