简体   繁体   English

如何访问在另一类中在一类中声明的全局变量?

[英]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. 我正在用Python和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. 但是,由于此结果屏幕与输入风的屏幕不同,因此无法将str(var)添加到kivy标签(未定义)。

This is also my first time using Kivy and Classes etc. Any advice is really appreciated. 这也是我第一次使用Kivy和Classes等。真的很感谢任何建议。 My python knowledge is not too great. 我的python知识不是很好。

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. 然后尝试将wind_Result全局化,以便能够在另一个类中调用它。

However, the wind_Velocity1(self) is called when a button is pressed. 但是,按下按钮时将调用wind_Velocity1(self)。

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. 相反,我将在您的__init__类中声明变量,然后可以在其他类中继承这些变量。

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: 在初始化您的类之后,您可以将self.<variable>作为类的属性进行访问:

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中,您可以继承 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: 在您的实际情况下,我将在类init中创建HWC1和XWC1`变量,然后您可以通过继承在所有其他类中访问它们:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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