Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to access properties(class variables) from another screen

Im using kivy, and i have two screens, one has class variables which are numeric properties and i have methods which change the value according to the situation. The second screen should be used to display final values to the user.

class Main(Screen):
    transport_price_value = NumericProperty(0)

    def change_value_on_press(self):
        self.transport_price_value += 10


class Second(Screen):
     def display_transport_price_value(self):
         return str(Main.transport_price_value)


class WindowManager(ScreenManager):
     pass


class Example(App):
     pass

When i click the submit button which triggers "display_transport_price_value" function it returns the name istead of the value of the property

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since Main is just a class and not an instance, and since in python it is possible to have multiple instances, what you really need is a way to get the Main instance created by kivy. I have not tested but I think this will work:

class Main(Screen):
    transport_price_value = NumericProperty(0)

    def change_value_on_press(self):
        self.transport_price_value += 10


class Second(Screen):
     def display_transport_price_value(self):
         main_screen = self.manager.get_screen("main")
         return str(main_screen.transport_price_value)


class WindowManager(ScreenManager):
     pass


class Example(App):
     pass

Source: https://stackoverflow.com/a/61002677/13314450

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading