python - 通过屏幕管理器kivy回收输入值
问题描述
我想从我的第一个屏幕捕获一个值到我的第三个屏幕。
首先,我在输入字段中写下我的名字。我去下一个窗口。
我尝试在最后一个窗口中显示我的名字。
所以我和你分享代码,我希望我能找到一个问题。
蟒蛇代码:
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
#define ou different screens
class FirstWindow(Screen):
def envoyer(self):
name = self.ids.nom_input.text
self.ids.my_name.text = name
class SecondWindow(Screen):
pass
class ThirdWindow(Screen):
#PROBLEM HERE
def on_press(self):
self.ids.recup_infos.text = self.root.get_screen('FirstWindow').ids.my_name.text
class WindowManager(ScreenManager):
pass
class MonWidget(Widget):
pass
kv = Builder.load_file('new_window.kv')
class AwesomeApp(App):
def build(self):
Window.clearcolor = (0,0,0,0)
return kv
if __name__ == '__main__':
AwesomeApp().run()
我的 KV 代码:
WindowManager:
FirstWindow:
SecondWindow:
ThirdWindow:
<FirstWindow>:
name: "romain"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
id: my_name
text: "Entrez votre nom"
font_size: 32
TextInput:
id: nom_input
multiline: False
size_hint: (1, .5)
Button:
text: "Next screen"
font_size: 32
on_press: root.envoyer()
on_release:
app.root.current = "Mickael"
root.manager.transition.direction = "left"
<SecondWindow>:
name: "Mickael"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "Entre votre ville"
font_size: 32
TextInput:
id: ville_input
multiline: False
size_hint: (1, .5)
Button:
text: "Vérifier les infos"
font_size: 32
on_release:
app.root.current = "foncier"
root.manager.transition.direction = "left"
Button:
text: "go back first screen"
font_size: 32
on_release:
app.root.current = "romain"
root.manager.transition.direction = "right"
<ThirdWindow>:
name: "foncier"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "Verifier : "
font_size: 32
Label:
id: recup_infos
text: ""
font_size: 32
color: 'white'
Button:
text: "On press"
font_size: 32
#Problem HERE
on_press: root.on_press()
Button:
text: "Précedent"
font_size: 32
on_release:
app.root.current = "Mickael"
root.manager.transition.direction = "right"
你可以帮帮我吗 ?谢谢罗曼
解决方案
在你的on_press
方法中:
def on_press(self):
self.ids.recup_infos.text = self.root.get_screen('FirstWindow').ids.my_name.text
self.root.get_screen('FirstWindow').ids.my_name.text
不是访问您现在所在的班级之外的小部件的正确方法,或者在这种情况下,屏幕。正确的方法是使用App.get_running_app()
方法:
self.ids.recup_infos.text = App.get_running_app().root.ids.First.ids.my_name.text
但在此之前,您必须为应用程序的屏幕提供 id,以便First
上面演示的方法的参数实际上是有意义的:
WindowManager:
FirstWindow:
id: First
# "First" is the id of the FirstWindow class
# which can also explain why there was a "First" arg
# inside "App.get_running_app().root.ids.First.ids.my_name.text"
SecondWindow:
id: Second
ThirdWindow:
id: Third
仍然对为什么这样做感到困惑?让我们将 的属性App.get_running_app().root.ids.First.ids.my_name.text
分为 3 个部分:
App.get_running_app()
:此方法返回您正在运行的 App 类的位置,在本例中为AwesomeApp
。这也就像self
您要在 App 对象本身中获取变量一样.root.ids.First
:如果你仔细阅读 Kivy 文档,或者只是简单地在线观看 Kivy 课程视频,你应该知道self.root.ids
在 App 对象内部会返回根小部件内小部件的 id 列表。在这种情况下,在App.get_running_app().root.ids
这里做同样的事情,并且您的屏幕在ScreenManager
根小部件中传递,因此First
在App.get_running_app().root.ids
.ids.my_name.text
:与上面App.get_running_app().root.ids.First
相同,self
就像您在FirstWindow
课堂上运行它一样,这使您有机会访问工作课程/屏幕之外的变量
推荐阅读
- android - Android:防止软键盘在聊天窗口中关闭
- numpy - 如何在 xarray resample 函数中获取每日最大值的确切坐标?
- javascript - 在字符串数组中使用正则表达式
- angular - 无法绑定到“formGroup”,因为它不是 Angular 8 中“form”的已知属性
- javascript - 按键中的多个函数,调用第二个函数,直到第一个函数完成
- lisp - 创建一个返回列表的函数,该列表包含列表的所有其他值
- python - python中属性中“str”对象的使用
- mobile - 在启动时导航到移动应用程序中的非主页是否可以接受
- javascript - 从授权服务器重定向后如何解决 CORS 错误?
- c - 将文本附加到 C 中文件的开头不起作用