How do we access the TextInput in the layout class from the main app class in Kivy?

Viewed 28

I am having a little doubt on a little Kivy project I'm doing, any help will be appreciated. I have made simple ToDo Pad where we can simply write things (nothing much special except a clear button and a switch theme button), now I want to save the data before quitting the application. The problem is the TextInput from where I am getting the data is actually in the my MainLayout class, but I want to save it when the app closes (so I'm running on_stop() function inside the ToDoPadApp class) and it doesn't let me because I'm not able to access TextInput in the MainLayout class from the ToDoPadApp class.

Python main file:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.utils import rgba


class MainLayout(Widget):
    text_input = ObjectProperty(None)       # this is the text input I need to access
    theme_button = ObjectProperty(None)
    clear_button = ObjectProperty(None)

    selected_theme = "dark"

    def on_theme_button_pressed(self):
        if(self.selected_theme == "dark"):
            # changing the theme variable
            self.selected_theme = "light"

            # changing the text color variable
            self.text_color = rgba(0, 0, 0)

            # changing colors for text input
            self.text_input.background_color = rgba(255, 255, 255)
            self.text_input.foreground_color = self.text_color

            # changing colors for theme button
            self.theme_button.background_color = rgba(64, 170, 64)
            self.theme_button.color = rgba(0, 0, 0)
        else:
            # changing the theme variable
            self.selected_theme = "dark"
            
            # changing the text color variable
            self.text_color = rgba(255, 255, 255)

            # changing colors for text input
            self.text_input.background_color = rgba(0, 0, 0)
            self.text_input.foreground_color = self.text_color

            # changing colors for theme button
            self.theme_button.background_color = rgba(46, 46, 127)
            self.theme_button.color = rgba(255, 255, 255)

    def on_clear_button_pressed(self):
        self.text_input.text = ""


class ToDoPadApp(App):
    def build(self):
        return MainLayout()

    def on_stop(self):
        # this is where I need to access that text input for saving it's data
        pass


if __name__ == '__main__':
    ToDoPadApp().run()

Kivy Design Code:

#: import rgba kivy.utils.rgba
<Button>:
    font_size: 25

<MainLayout>
    text_input: text_input
    theme_button: theme_button
    clear_button: clear_button

    text_color: rgba(255, 255, 255)

    GridLayout:
        size: root.width, root.height
        cols: 1

        TextInput:
            id: text_input
            background_color: rgba(0, 0, 0)
            font_size: 32
            on_text: self.foreground_color = root.text_color

        Button:
            size_hint: (1, None)
            height: 50

            id: theme_button
            text: "Switch Theme"
            background_normal: ''
            background_color: rgba(46, 46, 127)
            color: rgba(255, 255, 255)

            on_press: root.on_theme_button_pressed()

        Button:
            size_hint: (1, None)
            height: 50

            id: clear_button
            text: "[X] Clear"
            background_normal: ''
            background_color: rgba(255, 0, 0)
            color: rgba(255, 255, 255)

            on_press: root.on_clear_button_pressed()

Or is there any better way of accomplishing what I'm trying to do? Please help!

1 Answers

you can access text_input from the following code snippet.

def on_stop(self):
    # this is where I need to access that text input for saving it's data
    print(self.root.text_input.text)
Related