For this PyQt code, when I am clicking the corresponding button in the output GUI, it says:
对于这个PyQt代码,当我在输出GUI中单击相应的按钮时,它会说:
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"
4 个解决方案
#1
2
change
改变
n == data2
to
来
n = data2
and watch your error dissapear. :)
注意你的错误。:)
#2
1
Quite so. The first time n
occurs is
那么。第一次发生n。
n == data2
where it is not yet defined.
它还没有定义。
#3
0
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.
在赋值之前,n == data2行尝试访问n。这条线似乎并没有试图做一些有用的事情;如果你移除它,你的问题就会消失,不会有任何负面影响。
#4
0
The NameError is being raised because you're making the comparison n == data2
before n
is assigned a value.
之所以会出现NameError,是因为在给n赋值之前,您要让比较n == data2。
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.
global关键字只声明应该将以下变量添加到全局范围;您仍然需要为该变量指定一个值,以便使用它。
#1
2
change
改变
n == data2
to
来
n = data2
and watch your error dissapear. :)
注意你的错误。:)
#2
1
Quite so. The first time n
occurs is
那么。第一次发生n。
n == data2
where it is not yet defined.
它还没有定义。
#3
0
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.
在赋值之前,n == data2行尝试访问n。这条线似乎并没有试图做一些有用的事情;如果你移除它,你的问题就会消失,不会有任何负面影响。
#4
0
The NameError is being raised because you're making the comparison n == data2
before n
is assigned a value.
之所以会出现NameError,是因为在给n赋值之前,您要让比较n == data2。
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.
global关键字只声明应该将以下变量添加到全局范围;您仍然需要为该变量指定一个值,以便使用它。