Python 3.x 如何引用使用kv语言在kivy中创建的不同屏幕的小部件?

Python 3.x 如何引用使用kv语言在kivy中创建的不同屏幕的小部件?,python-3.x,kivy,kivy-language,Python 3.x,Kivy,Kivy Language,我的应用程序由两个屏幕组成: 屏幕1-1个文本输入小部件和1个按钮小部件 屏幕2-1文本输入小部件 在按下按钮时,我想捕获在Screen1的textinput中输入的数据,并在Screen2的textinput中打印它 我尝试了以下代码,但出现错误: main.py from kivy.app import App from kivy.lang import Builder from kivy.uix.screenmanager import ScreenManager, Screen from

我的应用程序由两个屏幕组成:

屏幕1-1个文本输入小部件和1个按钮小部件

屏幕2-1文本输入小部件

在按下按钮时,我想捕获在Screen1的textinput中输入的数据,并在Screen2的textinput中打印它

我尝试了以下代码,但出现错误:

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty

class Screen1(Screen):
    pass

class Screen2(Screen):
    pass  

class WindowManager(ScreenManager):
    txtinp1 = ObjectProperty(None)
    txtinp2 = ObjectProperty(None)

class ResultsApp(App):

    def build(self):
        return Builder.load_file("tutorials\Design3.kv")

    def disp(self):
        self.root.txtinp2.text = self.root.txtinp1.text        

if __name__ == "__main__":
    ResultsApp().run()
设计3.kv

WindowManager:
    Screen1:        
    Screen2:     

<Screen1>    
    name:'main'
    txtinp1:textinp1 
    GridLayout:
        cols:1                
        TextInput:
            id:textinp1
        Button:
            text:'Submit'
            on_press: 
                app.root.current = 'second'
                app.disp()
<Screen2>
    name:'second'
    txtinp2:textinp2 
    GridLayout:        
        cols:1        
        TextInput:
            id:textinp2


我试图在stackoverflow中寻找解决方案,但找不到。

通过执行
txtinp1:textinp1
您表示Screen1通过txtinp1属性映射textinp1对象,但是您希望通过根目录访问该属性,这显然是错误的

有很多解决方案,但在本例中,我将展示如何在ScreenManager中映射每个屏幕的属性:

WindowManager:
    txtinp1: screen1.textinp1
    txtinp2: screen2.textinp2
    Screen1: 
        id: screen1       
    Screen2:
        id: screen2


 <Screen1>    
    textinp1: textinp1
    name:'main'
    GridLayout:
        cols:1        
        TextInput:
            id:textinp1
        Button:
            text:'Submit'
            on_press: 
                app.root.current = 'second'
                app.disp()

 <Screen2>
    textinp2: textinp2
    name:'second'
    GridLayout:        
        cols:1        
        TextInput:
            id:textinp2
WindowManager:
txtinp1:screen1.textinp1
txtinp2:screen2.textinp2
屏幕1:
id:屏幕1
屏幕2:
id:屏幕2
<Screen1>
textinp1:textinp1
姓名:'main'
网格布局:
科尔斯:1
文本输入:
id:textinp1
按钮:
正文:“提交”
新闻界:
app.root.current='秒'
app.disp()
<Screen2>
textinp2:textinp2
姓名:'second'
网格布局:
科尔斯:1
文本输入:
id:textinp2
WindowManager:
    txtinp1: screen1.textinp1
    txtinp2: screen2.textinp2
    Screen1: 
        id: screen1       
    Screen2:
        id: screen2


 <Screen1>    
    textinp1: textinp1
    name:'main'
    GridLayout:
        cols:1        
        TextInput:
            id:textinp1
        Button:
            text:'Submit'
            on_press: 
                app.root.current = 'second'
                app.disp()

 <Screen2>
    textinp2: textinp2
    name:'second'
    GridLayout:        
        cols:1        
        TextInput:
            id:textinp2