Home > front end >  Is there a way to access properties(class variables) from another screen
Is there a way to access properties(class variables) from another screen

Time:04-10

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

CodePudding user response:

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

  • Related