python - 如何使用第二个屏幕从 kivy 的文本字段中提取文本?
问题描述
我最近开始学习 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
试图使用 the input
id
,就好像它在ids
of the root
(即 the ScreenManager
)中一样,但是字典是在定义它们ids
的规则的根中设置的。kv
所以input
id
是在ids
的WritingScreen
。要访问ids
,WritingScreen
您可以将该行代码更改为:
text = self.root.get_screen('Write').ids.input.text
推荐阅读
- hana - 如何使用 SQL 查询转义 Hana DB 密码中的特殊字符?
- linux - 如何在linux中使用bash shell脚本递归地重命名文件中的所有目录、文件和文本
- python - 如何使python selenium 不可检测
- nginx - Kubernetes - 使用 gRPC 的入口
- php - 在 PHP 中使用 STDIN 读取多行
- sql - 具有多种可能性的模式匹配sql
- python - sys.stdin.read() 和 sys.stdin.readline() 有什么区别
- javascript - 使用消息或警报返回注册页面
- c# - 尽管在 Web.Config 中设置了配置,但仍被定向到 HTTP 错误 404.13 页面
- ape-phylo - 在猿中运行 PGLS 时出错:“未指定协变量”