[英]'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:我的应用程序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]
The line of code:代码行:
self.root.ids.text_field.text = (''.join(f))
is trying to access the ids
of the app root.正在尝试访问应用程序根目录的ids
。 But the app root is the ScreenManager
, which has no ids
(because it does not appear in the kv
).但是应用根目录是ScreenManager
,它没有ids
(因为它没有出现在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.解决方法是访问Readed
Screen
的ids
,因为这是定义text_field
的地方。 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
.由于root
是ScreenManager
,您可以使用get_screen()
来获取对Readed
Screen
的引用。 Once you have that reference, you can use the ids
defined in the Screen
by the kv
.获得该参考后,您可以使用kv
在Screen
中定义的ids
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.