简体   繁体   中英

How to access global variables, declared in one class, during another class?

I'm making my first mobile phone and desktop application with Python and Kivy. The purpose of the app is to reduce time spent on planning calculations for training flights.

So, I have multiple classes, each for a different 'screen' on the app. On each screen, different inputs are made along the way. At the end, there is a 'results screen' which shall print out the calculations made during that section.

For example, the wind direction/velocity and runway direction is entered on one screen, and the results page prints out the headwind and crosswind components.

However, since this results screen is a different class to the screen on which the wind is entered, I cannot add the str(var) to the kivy label, as not defined.

This is also my first time using Kivy and Classes etc. Any advice is really appreciated. My python knowledge is not too great.

Many thanks!

I tried to define a function outside of all the classes. Ie

class Takeoff_3Window(Screen):

    def wind_Velocity1(self):
        global var
        ... code ...

wind_Result = Takeoff_3Window(Screen)
wind_Result.wind_Velocity()

and then trying making wind_Result global, to be able to call it in another class.

However, the wind_Velocity1(self) is called when a button is pressed.

class Takeoff_3Window(Screen):

    rwy1 = ObjectProperty(None)
    wd1 = ObjectProperty(None)
    wv1 = ObjectProperty(None)

    ...
    functions to input runway number, wind direction and wind velocity

    another function, called advance_TO3() checks all the other inputs were valid
    ...

    def TO_4Btn(self, math):
        global HWC1, XWC1
        self.advance_TO3()
        if advance_TO3 is True:
            HWC1 = round((WV1 * math.cos(math.radians(abs(RWY1 - WD1)))), 1)
            XWC1 = round((WV1 * math.sin(math.radians(abs(RWY1 - WD1)))), 1)
            sm.current = "SurfaceDetails"
        else:
            pass

...
further classes (screens) take other input data from the user, similar to above
...

class Takeoff_ResultWindow(Screen):

    tor_tom = ObjectProperty(None)
    tor_cog = ObjectProperty(None)
    tor_elev = ObjectProperty(None)
    tor_wv = ObjectProperty(None)
    tor_qnh = ObjectProperty(None)
    tor_hwc = ObjectProperty(None)
    tor_palt = ObjectProperty(None)
    tor_xwc = ObjectProperty(None)
    tor_temp = ObjectProperty(None)
    tor_rwy = ObjectProperty(None)

    def TOR_Page1(self):
        pressureAlt1 = 0
        self.tor_tom.text = "TOM: " + str(TOM)
        self.tor_cog.text = "Centre of Gravity: " + str((TO_Moment*1000)/TOM)
        self.tor_elev.text = "Elevation: " + str(Elevation1)
        self.tor_wv.text = "Wind Velocity: " + str(WV1)
        self.tor_qnh.text = "QNH: " + str(QNH_1)
        self.tor_hwc.text = "Wind (HWC): " + str(HWC1)
        self.tor_palt.text = "Pressure Alt.: " + str(pressureAlt1)
        self.tor_xwc.text = "Wind (XWC): " + str(XWC1)
        self.tor_temp.text = "Temperature: " + str(Temp1)
        self.tor_rwy.text = "Runway: " + str(RWY1)

Thank you!

I'm not really sure what you are trying to accomplish with your global variables, so I am taking a shot in the dark here.

I would recommend not using global variables. Instead I would declare the variables in your class __init__ , then you can inherit those variables in your other classes.

For example:

ClassA:
    def __init__(self, foo):
        self.foo = foo
        self.bar = 'some_value'
        self.foobar = 'abcd'

# class initialization
classA = ClassA('some_other_value') #this will set self.foo = 'some_other_value'

After initializing your class, you can access self.<variable> as a property of the class:

classA = ClassA('some_other_value')
print(classA.foo) # prints some_other_value

Now, in your ClassB, you can inherit all the properties of ClassA:

ClassB(Screen, ClassA):
    def __init__(self, foo):
        ClassA.__init__(self, foo)
        self.bar = 'some_new_value' #you can override properties from ClassA like this

classB = ClassB('some_other_value')

print(classB.foo) # prints 'some_other_value'
print(classB.bar) # prints 'some_new_value'
print(classB.foobar) #prints 'abcd'

In your exact situation, I would create the HWC1 and XWC1` variables inside of your class init, then you can access them in all of your other classes via inheritance:

class Takeoff_3Window(Screen):
    def __init__(self):
        rwy1 = ObjectProperty(None)
        wd1 = ObjectProperty(None)
        wv1 = ObjectProperty(None)
        self.HWC1 = round((WV1 * math.cos(math.radians(abs(RWY1 - WD1)))), 1)
        self.XWC1 = round((WV1 * math.sin(math.radians(abs(RWY1 - WD1)))), 1)

Now in your other class:

class Takeoff_ResultWindow(Screen, Takeoff_3Window):
    def __init__(self):
        print(self.HWC1)
        print(self.XWC1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM