<p>问题是每次遍历<code>self.createShotBtn</code>、<code>self.labelName</code>和{<cd3>}的值都会被重写。在</p>
<p>因此,在最后一次运行时,它们是固定的,但只适用于最后一次迭代。在</p>
<p>相反,您希望在循环中使用一个局部范围的变量,并可能将其存储在一个数组中以供以后使用。在</p>
<p>这段代码应该接近您需要的,但是我可以看到<code>self.shotNumberLst</code>(它返回一个数字?)和<code>shotjump</code>(这是offest,或等于<code>i</code>)被声明。在</p>
<pre><code>self.shots = []
for i in range(seqNum): # Changed while to for, so you don't need to increment
#create each widget
createShotBtn = QtGui.QPushButton("Create Shot %s!" %str(self.shotNumberLst[i-1]))
labelName = QtGui.QLabel(self)
labelName.setText("Enter Name Of Shot %s!" %str(self.shotNumberLst[i-1]))
shotName = QtGui.QLineEdit(self)
self.shots.append({"button":createShotBtn,
"name":shotName)) # Store for later if needed.
#add widget to layout
self.grid.addWidget(labelName, 11+shotjump,0)
self.grid.addWidget(shotName,11+shotjump,1)
self.grid.addWidget(createShotBtn, 11+shotjump,2)
#Press button that makes magic happen
createShotBtn.clicked.connect(self.createShot(i))
#elsewhere
def createShot(self,index):
print self.shots[index]["name"].text
</code></pre>