这个问题在这里已有答案:
我正在使用python创建一个项目,它可以像excel一样,所以当我在单元格中输入一些数字时,它会自动计算出来
从我之前的问题我再次尝试了一些代码但是当我尝试使用一些先前的列值自动计算当前列值时卡住了。
计算PGA值的代码(与IKS相同,只是略有不同)......
def on_itemChanged(self,item):
if item.column() in (0, 1):
self.calculate_pga(item.row())
def calculate_pga(self, row):
self.tablewidget.blockSignals(True)
for col in (0, 1):
ut = self.tablewidget.item(row, col)
if ut is None:
ut = QtWidgets.QTableWidgetItem("0")
self.tablewidget.setItem(row, col, ut)
self.tablewidget.blockSignals(False)
ut_x = self.tablewidget.item(row, 0)
ut_y = self.tablewidget.item(row, 1)
x = float(ut_x.text())
y = float(ut_y.text())
pga = x + y
ut_pga = self.tablewidget.item(row, 4)
if ut_pga is None:
ut_pga = QtWidgets.QTableWidgetItem()
self.tablewidget.setItem(row, 4, ut_pga)
ut_pga.setText(str(pga))
当公式为(IKS * PGA)时,我可以获得GSS值的正确方法是什么? This is the table
# Find indexes of column PGA, IKS and GSS
headercount = self.dlg.tableWidget.columnCount()
for x in range(headercount):
headertext = self.tableWidget.horizontalHeaderItem(x).text()
if 'PGA' == headertext:
self.matchcol_pga = x
if 'IKS' == headertext:
self.matchcol_iks = x
if 'GSS' == headertext:
self.matchcol_gss = x
# Set signal and call function on itemChanged
self.tableWidget.itemChanged.connect(self.changeResult)
def changeResult(self, item):
row = item.row()
self.dlg.tableWidget.blockSignals(True)
try:
pga = self.dlg.tableWidget.item(row, self.matchcol_pga).text() # get cell at row, col
iks = self.dlg.tableWidget.item(row, self.matchcol_iks).text() # get cell at row, col
gss = int(pga) * int(iks)
self.dlg.tableWidget.setItem(row, self.matchcol_gss, QTableWidgetItem(str(gss)))
except:
pass
self.dlg.tableWidget.blockSignals(False)
注意:使用blockSignals来避免 - 返回错误:RuntimeError:调用Python对象时超出了最大递归深度