Home > Mobile >  Retrieve variable value in another class
Retrieve variable value in another class

Time:03-08

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

CodePudding user response:

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'.
  • Related