'Use inputs of screen #1 as label text in screen #2

I'm trying to use the inputs collected from Screen 'size_screen' in the Screen 'game_screen'. I know usually people use the IDs from the kv file to use the .text of the inputs.

In my case I want to make all the design in the Python file as I will need to add and remove widgets dynamically on the screen and, at the moment, making the design on the py file I find it easier to manage.

But at the moment I haven't been able to understand how to use data from one screen into another.

Here below is a sample of the design I am doing for testing.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput


class game_screen(Screen):
    def __init__(self, **kwargs):
        super(game_screen, self).__init__(**kwargs)
        self.resize()

    def resize(self):
        mainlayout = BoxLayout(size=self.size, orientation="vertical")
        lbl1 = Label(text="HERE I WANT TO SEE THE TEXT INPUT 1")
        lbl2 = Label(text="HERE I WANT TO SEE THE TEXT INPUT 2")
        mainlayout.add_widget(lbl1)
        mainlayout.add_widget(lbl2)
        self.add_widget(mainlayout)


class size_screen(Screen):
    def __init__(self, **kwargs):
        super(size_screen, self).__init__(**kwargs)
        self.resize()

    def resize(self):
        mainlayout =BoxLayout (size=self.size, orientation="vertical")
        coordinates_input = GridLayout(size=self.size, cols=2, rows=2)
        lbl1 = Label(text="X")
        lbl2 = Label(text="Y")
        txi1 = TextInput()
        txi2 = TextInput()
        coordinates_input.add_widget(lbl1)
        coordinates_input.add_widget(txi1)
        coordinates_input.add_widget(lbl2)
        coordinates_input.add_widget(txi2)
        mainlayout.add_widget(coordinates_input)
        btn = Button(text="Create Life!",
                     on_release=self.go_to_game)
        mainlayout.add_widget(btn)
        self.add_widget(mainlayout)

    def go_to_game(self, obj):
        sm.current = "game"


sm = ScreenManager()
sm.add_widget(game_screen(name="game"))
sm.add_widget(size_screen(name="size"))
sm.current = "size"


class GameOfLifeApp(App):
     def build(self):
         return sm


GameOfLifeApp().run()


Solution 1:[1]

One of the many ways this could be done#1 as follows:

First create reference(s)#2 for each of the required widget(s) in each screen (actually most of the time it's a good idea) as,

    # In game_screen.
    def resize(self):
        mainlayout = BoxLayout(size=self.size, orientation="vertical")
        self.lbl1 = Label(text="HERE I WANT TO SEE THE TEXT INPUT 1")
        mainlayout.add_widget(self.lbl1)
        ...

    # In size_screen.
    def resize(self):
        ...
        coordinates_input = GridLayout(size=self.size, cols=2, rows=2)
        self.txi1 = TextInput()
        coordinates_input.add_widget(self.txi1)
        ...

Now in the method go_to_game,

    def go_to_game(self, obj):
        sm.current = "game"
        # Access the required screen.
        g_screen = sm.get_screen("game")
        # Get and set the required value.
        g_screen.lbl1.text = self.txi1.text

#1: For your current design.
#2: ObjectProperty could've been used as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1