NameError: name is not defined

NameError: name is not defined

更新的代码片段: 我已经更新到 'self.' 约定而不是全局约定,但仍然有唯一的值返回在 class 级别上分配的空字符串,但如果删除它,则会引发错误。

class MainDialog(QWidget, qcDbWidget4.Ui_qcQueryWidget):

    dbDir = ''
    DbSelection = ''
    dbPath = ''
    TabSelection = ''
    exportDir = ''
    exportFileName = ''  

    def __init__(self, parent=None):
        super(MainDialog, self).__init__(parent)
        self.setupUi(self)
        self.connect(self.dbDirSelect, SIGNAL("clicked()"), self.getDirFileNames)
        self.connect(self.dbSelectButton, SIGNAL('clicked()'), self.selectDb)
        self.connect(self.tabSelectButton, SIGNAL('clicked()'), self.selectTab)
        self.connect(self.exportDirSelect, SIGNAL("clicked()"), self.getExportDir)
        self.connect(self.exportButton, SIGNAL("clicked()"), self.setExportName)
        self.connect(self.querySubmitButton, SIGNAL("clicked()"), self.setQueryParams)
        self.connect(self.querySubmitButton, SIGNAL("clicked()"), self.dbQuery)

    def getDirFileNames(self):
        self.dbDir = str(QFileDialog.getExistingDirectory(self))
        self.dbDirDisplay.setText(self.dbDir)

        dbFileList = []
        for root, dirs, files in os.walk(self.dbDir):
            for file in files:
                if file.endswith('.db'):
                    dbFileList.append(file)

        self.dbSelection.addItems(dbFileList)

当您在 class 级别定义变量时,例如

class A(object):
    c = ''

您可以将 'c' 用作静态 class 变量或实例变量(或两者兼而有之,但这会造成混淆)。如果你想将它用作静态 class 变量,你需要将它用作 A.c(整个 class 的静态变量,可在实例方法之外访问)。如果你想将它用作实例变量,你需要使用 self.c (每个实例都有自己的变量)。 c 在这种情况下不会被定义为变量,如果你尝试使用 global c 你应该有 NameError.