0

I am writing an app with two screens using ScreenManager. In one screen I have a text input, and a button that reads such text input. If a certain condition from the input is satisfied, the second screen is activated. From this screen, I want to be able to grab the content of the text input from the first screen.

I have made multiple attempts and looked at many similar questions (this one for example), but none of them really seemed to work.

Below is a minimal non-working version of my code:

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


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

        box = BoxLayout()
        self.add_widget(box)

        self.searchInput = TextInput(multiline=False)
        box.add_widget(self.searchInput)

        self.searchButton = Button(text="Search")
        self.searchButton.bind(on_press=self.searchRecipe)                                  
        box.add_widget(self.searchButton)

    def searchRecipe(self, instance):
        src = self.searchInput.text
        if not src == "Go":
            pass
        else:  
            WMan.current = 'result'


class ResultWindow(Screen):
    def __init__(self, **kwargs): 
        super(ResultWindow, self).__init__(**kwargs)
        titleBox = BoxLayout()
        self.add_widget(titleBox)        

        print(src)


WMan = ScreenManager()
WMan.add_widget(RootWindow(name='root'))  
WMan.add_widget(ResultWindow(name='result'))


class RecipApp(App):
    def build(self):
        return WMan


if __name__ == "__main__":
    RecipApp().run()
gicanzo
  • 69
  • 8

1 Answers1

0

If you save a reference to titlebox like this:

class ResultWindow(Screen):
    def __init__(self, **kwargs): 
        super(ResultWindow, self).__init__(**kwargs)
        self.titleBox = BoxLayout()
        self.add_widget(self.titleBox)        

Then you can access the titlebox via the WMan, like this:

def searchRecipe(self, instance):
    src = self.searchInput.text
    if not src == "Go":
        pass
    else:
        WMan.current = 'result'
        WMan.current_screen.titlebox.add_widget(Label(text=src))
John Anderson
  • 35,991
  • 4
  • 13
  • 36
  • How to put that label into ResultWindow.titleBox? I tried: ```WMan.current_screen.titleBox.add_widget(Label(text=src))``` but that didn't work. – gicanzo May 03 '20 at 20:40
  • I had tried this already. However, although it does not give any error, it does not produce any result (i.e. the label is not added) – gicanzo May 04 '20 at 14:53
  • Note that your logic in the `searchRecipe()` method will not add a `Label` unless the `src` is "Go". – John Anderson May 04 '20 at 17:39
  • well, I know, that's exactly what I want the method to do. I posted here just an example inspired to my code, not my actual code. In my actual code, the textinput is a search form. So I check the input against a SQLite database, and if a match is found, I print the results (the src in this example) in another screen (ResultWindow) – gicanzo May 04 '20 at 21:07