繁体   English   中英

'super' object 没有属性 '__getattr__',谁能帮帮我?

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

进口:

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 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]

代码行:

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

正在尝试访问应用程序根目录的ids 但是应用根目录是ScreenManager ,它没有ids (因为它没有出现在kv中)。 这就是导致错误的原因。

解决方法是访问Readed Screenids ,因为这是定义text_field的地方。 尝试类似:

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

由于rootScreenManager ,您可以使用get_screen()来获取对Readed Screen的引用。 获得该参考后,您可以使用kvScreen中定义的ids

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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