繁体   English   中英

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

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

我正在用Python和Kivy制作我的第一个手机和桌面应用程序。 该应用程序的目的是减少用于训练飞行计划计算的时间。

因此,我有多个类,每个类针对应用程序上的不同“屏幕”。 在每个屏幕上,一路进行不同的输入。 最后,有一个“结果屏幕”,该屏幕将打印出在此部分进行的计算。

例如,在一个屏幕上输入风向/速度和跑道方向,结果页将打印出逆风和侧风分量。

但是,由于此结果屏幕与输入风的屏幕不同,因此无法将str(var)添加到kivy标签(未定义)。

这也是我第一次使用Kivy和Classes等。真的很感谢任何建议。 我的python知识不是很好。

非常感谢!

我试图在所有类之外定义一个函数。

class Takeoff_3Window(Screen):

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

wind_Result = Takeoff_3Window(Screen)
wind_Result.wind_Velocity()

然后尝试将wind_Result全局化,以便能够在另一个类中调用它。

但是,按下按钮时将调用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)

谢谢!

我不太确定您要使用全局变量来做什么,所以我在这里暗中摸索。

我建议不要使用全局变量。 相反,我将在您的__init__类中声明变量,然后可以在其他类中继承这些变量。

例如:

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'

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

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

现在,在您的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'

在您的实际情况下,我将在类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)

现在在您的其他班级:

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