sparrow
sparrow

Reputation: 297

NameError: global name 'n' is not defined

For this PyQt code, when I am clicking the corresponding button in the output GUI, it says:

NameError: global name 'n' is not defined

Here is the code:

class MyDialog5(QDialog):
    def __init__(self, parent=None):
        super(MyDialog5, self).__init__(parent)
        self.setGeometry(360,380,360,380)
        self.setWindowTitle('Distribution')
        self.table = QTableWidget()
        self.table.setColumnCount(2)
        self.table.setRowCount(10)
        self.table.setAlternatingRowColors(True)
        self.table.setItem(11,1, QTableWidgetItem('224'))
        self.table.setSelectionBehavior(QTableWidget.SelectRows)
        self.table.setHorizontalHeaderLabels(["x", "y"])

        data1 = ['blue', 'red']
        data2 = ['1','1']
        for i in range(2):
            item1 = QTableWidgetItem(data1[i])
            self.table.setItem(i,0,item1)
            item1.setFlags(Qt.ItemIsEditable)
            item1.setForeground(QColor('black')) 
            item2 = QTableWidgetItem(data2[i])
            self.table.setItem(i,1,item2)
            item2.setForeground(QColor('black')) 

        self.pbutton1 = QPushButton('clicke here')
        self.pbutton1.clicked.connect(self.on_pbutton1_clicked)
        self.pbutton2 = QPushButton('Set Defaults')
        rightLayout = QVBoxLayout()
        rightLayout.addWidget(self.pbutton1)
        rightLayout.addWidget(self.pbutton2)
        rightLayout.addStretch(1)
        self.buttonBox = QDialogButtonBox(self)
        self.buttonBox.setOrientation(Qt.Horizontal)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel|
                                            QDialogButtonBox.Ok)
        mainLayout = QHBoxLayout()
        mainLayout.addWidget(self.table)
        mainLayout.addLayout(rightLayout)
        self.verticalLayout = QVBoxLayout(self)
        self.verticalLayout.addLayout(mainLayout)
        self.verticalLayout.addWidget(self.buttonBox)

    def on_pbutton1_clicked(self):
        global n
        n == data2
        n = [int(i) for i in n]
        if sum(n) != 0:
               print "none"

Upvotes: 0

Views: 2124

Answers (4)

jgray
jgray

Reputation: 76

The NameError is being raised because you're making the comparison n == data2 before n is assigned a value.

The global keyword only declares that the following variable should be added to the global scope; you still need to assign the variable a value in order for it to be used.

Upvotes: 0

change

n == data2

to

n = data2

and watch your error dissapear. :)

Upvotes: 2

kojiro
kojiro

Reputation: 77137

Quite so. The first time n occurs is

n == data2

where it is not yet defined.

Upvotes: 1

icktoofay
icktoofay

Reputation: 129059

The n == data2 line tries to access n before it has been assigned. That line doesn’t appear to be trying to do something useful, either; if you remove it, your problem should go away without any negative effects.

Upvotes: 0

Related Questions