获取 Kivy 属性错误“super”对象没有属性“__getattr__”(Python)

问题描述 投票:0回答:1
TraceBack error:
Exception has occurred: AttributeError
'super' object has no attribute '__getattr__'
KeyError: 'text_label'

During handling of the above exception, another exception occurred:

  File "/Users/samarth/Downloads/App/main_app.py", line 29, in correct
    self.ids.text_label.text = "Correct"
  File "/Users/samarth/Downloads/App/main_app.py", line 383, in button_press
    MyLayout().correct()
  File "/Users/samarth/Downloads/App/color.kv", line 26, in <module>
    on_press: app.button_press()
  File "/Users/samarth/Downloads/App/main_app.py", line 410, in <module>
    colorApp().run()
AttributeError: 'super' object has no attribute '__getattr__'

收到错误:'super'对象没有属性'getattr' 不知道为什么会发生这种情况,我在 button_press 函数末尾调用了一个函数,当文件尝试运行该函数时会出现此错误(两个函数位于不同的类中)。我正在尝试动态更新标签的文本。我已经尝试过 StackOverflow 上人们提供的其他解决方案,但它们对我不起作用(或者可能是我做错了)。

代码(删除了一些无用的部分):

Python:

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder 
from kivy.uix.image import Image
import random
import os
from kivy.core.window import Window
import numpy as np
from kivy.properties import StringProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
imp = "x"
user_inp = "x"


class MyLayout(Widget):
    #THIS IS WHERE THE ERROR SEEMS TO OCCUR
    def correct(self):

        self.ids.text_label.text = "Correct"
        return RootLayout()
    def incorrect(self):
        self.ids.text_label.text = "Incorrect"
        return RootLayout()
        

class RootLayout(FloatLayout):
    pass

prior = StringProperty()

class colorApp(App, FloatLayout):

    flagfile = StringProperty("0")
    
    path = "/Users/sam/Downloads/App/w320"
    files = os.listdir(path)
    #self.important = random.choice(files)
    important = "bt.png"
    flagfile= path + "/" + important
    
    other_answer = StringProperty()
  
    prior = StringProperty()
    def button_press(self):
        #[Removed Code]
 
        print(self.correct_answer)
        self.imp = self.correct_answer
        
        self.prior = self.root.ids.user_input.text.lower()

        user_inp = self.prior

        #THIS IS WHERE I CALL THE FUNCTION <------
        if user_inp == self.imp:   
            MyLayout().correct()
        else:
            MyLayout().incorrect()
        
        print(self.prior)
        print(self.imp)
        print(self.important)
        print(user_inp)

        return RootLayout()

    def build(self):
        return RootLayout()

    
if __name__ == "__main__":
    colorApp().run()

基维

<RootLayout>
    MyLayout:  
        BoxLayout:
            orientation: "vertical"
            size: root.width, root.height

            padding: 50
            spacing: 20
            Image:
                id: cardimage
                #source: app.flagfile
                source: "w320/bt.png"
            
            TextInput:
                multiline: False
                text: "Enter Guess Here"
                on_focus: self.text = "" if args[1] else self.text
                id: user_input
                
            Button:
                text: "Submit"
                font_size: 32
                on_press: app.button_press()
            
        FloatLayout:
            Label:
                text:'32'
                pos: 1200, 910
                id: chill
                font_size: 75
                
python python-3.x kivy label kivy-language
1个回答
0
投票

试试这个:

if user_inp == self.imp:   
        self.correct()
    else:
        self.incorrect()

如果这不起作用,请尝试不要创建 MyLayout 的新实例,而只需引用一个:

if user_inp == self.imp:   
    MyLayout.correct()
else:
    MyLayout.incorrect()

在所有情况下回溯都会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.