Search code examples
pythonkivytextinput

Screen Manager set/get TextInput when button pressed


All I'm trying to do is get and/or set the input from the TextInput: inside the GridLayout, inside the SeccondWindow. (id: "text_input") But after googling, and googling... all my attempts have failed.

I have tried

main_screen = self.manager.get_screen('second')
main_screen.ids.text_input.text = "Something..."

Only to get the error "AttributeError: 'super' object has no attribute 'getattr'"

I've tried

text = self.root.ids["text_input"].text

But I get the error "AttributeError: 'SecondWindow' object has no attribute 'root'"

I've tried loads of things... I'M GOING INSANE!! (And I'm dumb so please help!)

Here's my new_window.kv file

WindowManager:
    transition: NoTransition()
    FirstWindow:
    SecondWindow:


<FirstWindow>:
    name: "first"

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height

        Button:
            text: "Go To Next Screen"
            on_release:
                app.root.current = "second"


<SecondWindow>:
    name: "second"
    GridLayout:
        id: "Container"
        cols: 2
        rows: 1

        ScrollView:
            id: "SideMenuScrollView"
            size_hint: ("0.3dp", 1)
            do_scroll_y: True
            do_scroll_x: False

            StackLayout:
                id: "SideMenuStack"
                size_hint_y: None
                height: self.minimum_height

                Button:
                    size_hint: (None, None)
                    size: ("92dp", "92dp")

                Button:
                    size_hint: (None, None)
                    size: ("92dp", "92dp")


        GridLayout:
            size_hint: (1, 1)
            id: "MyGrid"
            size: (1, 1)
            spacing: 10
            padding: 10
            cols: 1
            rows: 2

            TextInput:
                id: "text_input"
                multiline: False
                text: ""
                size_hint: (1, None)
                height: "30dp"


            Button:
                text: "Do Stuff"
                on_release: root.DoStuff()
                size_hint: (1, None)
                height: "70dp"

Here's my code

from kivy.app import App
from kivy.uix.screenmana.........

class WindowManager(ScreenManager):
    pass


class FirstWindow(Screen):
    pass


class SecondWindow(Screen):
    def DoStuff(self):

        #
        # text_input.text = "what ever..."
        #


kv = Builder.load_file('new_window.kv')


class AwesomeApp(App):
    def build(self):
        return kv


if __name__ == "__main__":
    AwesomeApp().run()

Does anyone know how to get/set the TextInput .text value and DoStuff with it??


Solution

  • The main problem is in your kv file:

            TextInput:
                id: "text_input"
    

    If you define an id with enclosing ", those " become part of the id. You can do that, but it complicates accessing that id. A simpler approach is to just eliminate the ", like this:

            TextInput:
                id: text_input
    

    Then you can access the TextInput as:

    class SecondWindow(Screen):
        def DoStuff(self):
            self.ids.text_input.text = 'Abba'