我有导出到 excel 函数和一堆 QTableWidgets。我知道如果我为每个 tablewidget 创建每个函数是非常低效的,但是我如何将 tablewidget 作为函数的参数传递?
def writeCsv(self):
path, _ = QFileDialog.getSaveFileName(self, 'Save File', QDir.homePath() + "/export.csv", "CSV Files(*.csv *.txt)")
if path:
with open(path, 'w') as stream:
print("saving", path)
writer = csv.writer(stream, dialect = 'excel', delimiter = ',')
headers = []
for column in range(self.tableWidget_show.columnCount()):
header = self.tableWidget_show.horizontalHeaderItem(column)
if header is not None:
headers.append(header.text())
else:
headers.append("Column " + str(column))
writer.writerow(headers)
for row in range(self.tableWidget_show.rowCount()):
rowdata = []
for column in range(self.tableWidget_show.columnCount()):
item = self.tableWidget_show.item(row, column)
if item is not None:
rowdata.append(item.text())
else:
rowdata.append('')
writer.writerow(rowdata)
我想使用self.tableWidget_showpart 作为参数,这样每次当我有其他参数时我都可以更改参数Qtablewidget
aluckdog
相关分类