簡體   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