Search code examples
python-3.xkivykivymdpyrebase

Retrieve variable value in another class


I'm using kivy + kivyMD and pyrebase, to do the login game or return from Firebase in a "user" variable, I want to use this variable to reconnect at any time inside the Login class.

Or problem is when I do or login correctly it directs me to another class and then I can't recover this value anymore.

Below is an example that is minimalist to guide me that can help me:

On line 117 I have a variable with any value. I would like to use this variable in a class that I HAVE to create in the fabric "scr 1" in line 44 (Note that I still do not create a class because I am giving a minimalist example)

I would like to obtain this value through the python code and not in the kv file.

reduced example


Solution

  • You may achieve that as follows :

    1. Create an ObjectProperty in MainScreen to refer the target label as,
    class MainScreen(Screen):
        user_name = ObjectProperty()
    
    1. Then in kvlang of MainScreen,
    <MainScreen>:
        user_name: user_name
        .
        .
        .
                Screen:
                    name: "scr 1"
    
                    MDLabel:
                        id: user_name
        .
        .
        .
    
    1. Now in method login of class TelaLogin,
        def login(self):
            user = 'Helo im user'
            manager = self.manager
            manager.current = 'main'
            manager.current_screen.user_name.text = user
            # You can also use method 'get_screen'.