简体   繁体   中英

'super' object has no attribute '__getattr__', can anyone help me?

imports:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
kv = '''

<Login>
    ben: benName.text
    pw: passwort.text
    knopf: btn
    knopff: btnn
 
    GridLayout:
        cols: 1
        size: root.width,root.height
        GridLayout:
            cols: 2
            Label:
                text: "Username"
                font_size: 25
            TextInput:
                id: benName
                multiline: False
                font_size: 30
            Label:
                text: "Password"
                font_size: 25
                bold: True
            TextInput:
                password: True
                id: passwort
                multiline: False
                font_size: 40
        Button:
            size_hint: (1.,1.10)
            text:" Start "
            id: btn
            font_size: 40
            on_release:
                root.manager.current = "Readed" if passwort.text == "1" and benName.text == "1" else "login"
                root.manager.transition.direction = "down"
        Button:
            size_hint: (1.,1.10)
            text: " Exit "
            id: btnn
            font_size: 40
            on_release: app.stop()






<readed>:
    BoxLayout:
        orientation: 'vertical'
        TextInput:
            id: text_field
        Button:
            text: 'click'
            on_press: app.read()

'''

MyApp class:

class Login(Screen):
    ben = StringProperty()
    pw = StringProperty()
    knopf = ObjectProperty()


class MyApp(App):
    Builder.load_string(kv)
    def read(self):
        file = open("read.txt", 'r')
        f = file.readlines()
        self.root.ids.text_field.text = (''.join(f))
        text = StringProperty("read.txt")
    def build(self):
        ms = ScreenManager()
        ms.add_widget(Login(name='login'))
        ms.add_widget(Readed(name='Readed'))
        self.title = "MyApp"
        return ms





class Readed(Screen):
    pass




if __name__ == '__main__':
    MyApp().run()
# her is the erorr
 #exec(__kvlang__.co_value, idmap)
   #File "<string>", line 59, in <module>
   #File "C:\Users\---\--\Desktop\B-PYTHON\----\TEST 45.py", line 93, in read
     #self.root.ids.text_field.text = (''.join(f))
   #File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
 #AttributeError: 'super' object has no attribute '__getattr__'
#[Finished in 5.9s with exit code 1]

The line of code:

self.root.ids.text_field.text = (''.join(f))

is trying to access the ids of the app root. But the app root is the ScreenManager , which has no ids (because it does not appear in the kv ). That is what causes the error.

The fix is to access the ids of the Readed Screen , because that is where the text_field is defined. Try something like:

self.root.get_screen('Readed').ids.text_field = (''.join(f))

Since root is the ScreenManager , you can use get_screen() to get a reference to the Readed Screen . Once you have that reference, you can use the ids defined in the Screen by the kv .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM