在Python中更新类之间的全局变量的问题

时间:2017-05-24 20:57:37

标签: python

使用ArcGIS Python Addin我需要在类之间更新名为phase的全局变量。在这里,我想要做的是更新phase变量,方法是更改​​ComboBoxClass1并点击ButtonClass3访问它。

正如您所见,我试图通过global phase = selection

    def onSelChange(self, selection):
        global phase = selection

但看起来这样不起作用!

import arcpy
import pythonaddins
import os

phase = ""

class ButtonClass3(object):
    """Implementation for Trace_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        print global phase

class ComboBoxClass1(object):
    """Implementation for Trace_addin.combobox (ComboBox)"""
    def __init__(self):
        self.items = ["Phase A", "Phase B", "Phase C"]
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWWWWWWW'
    def onSelChange(self, selection):
        global phase = selection
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        pass
    def onEnter(self):
        pass
    def refresh(self):
        pass

1 个答案:

答案 0 :(得分:2)

正如@jasonharper所说global phase本身就是一个声明 如果您想访问phase,则不必使用global例如:

print phase

如果您想修改phase,请使用:

global phase
phase = selection