![](/img/trans.png)
[英]Using Kivy screens, how do I change the label from screen 2 based on the pressed button's text on screen 1?
[英]How do I pull text from an text field in kivy using a second screen?
我最近开始学习 python 和 kivy,出于某种原因,我一直在学习
AttributeError: 'super' object has no attribute '__getattr__
'
目前我拥有的代码只是用于测试它,我知道存在其他类似的帖子,但我已经按照他们正在做的事情来修复它,我一直收到同样的错误。
这部分是text.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_file('text.kv')
# Declare both screens
class MenuScreen(Screen):
pass
class WritingScreen(Screen):
pass
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(WritingScreen(name='Write'))
screens = [Screen(name='Title {}'.format(i)) for i in range(2)]
class TestApp(App):
def build(self):
return sm
def write(self):
sm.current = 'Write'
def process(self):
text = self.root.ids.input.text
print("Hello")
if __name__ == '__main__':
TestApp().run()
这是我的 Text.kv 文件
<MenuScreen>:
FloatLayout:
Button:
text: 'Write'
on_press: app.write()
size_hint: .3, .2
background_color: 1, 2, 1, 1
pos_hint: {'x': .35, 'y': .4}
<WritingScreen>:
FloatLayout:
TextInput:
id: input
hint_text: "Ello There... Yaren't supposed to be here yet..."
size_hint: .8, .1
pos_hint: {'x':0, 'y': 0.9}
Button:
text: 'print'
on_press: app.process()
size_hint: .2, .1
pos_hint: {'x':.8, 'y': .8}
Button:
text: 'Back to menu'
on_press: root.manager.current = 'menu'
size_hint: .2, .1
pos_hint: {'x':.8, 'y': .9}
如果这是一个重复的问题,我再次感到很抱歉,但我尝试过的每一篇文章都对我不起作用。 感谢您的所有帮助,我仍然是初学者,如果有任何提示,我将不胜感激::)
您在process()
方法中的行:
text = self.root.ids.input.text
正在尝试使用input
id
,就好像它在root
(即ScreenManager
)的ids
ids
是在定义它们的kv
规则的根中设置的。 所以input
id
在WritingScreen
的ids
中。 要访问WritingScreen
的ids
,您可以将该行代码更改为:
text = self.root.get_screen('Write').ids.input.text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.