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

zphenhs4  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(116)
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' object has no attribute '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: text_label
                font_size: 75

nfg76nw0

nfg76nw01#

试试这个:

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

字符串
如果这不起作用,请尝试不要创建MyLayout的新示例,而只是引用一个:

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


在所有情况下,Traceback都会有帮助。

相关问题