admin管理员组文章数量:1287810
I used a simple method to load a table from Excel into the interface
def getExcelFile(self):
excel_file, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open file", downloads_path, "Excel files (*.xlsx)")
if excel_file != "":
self.mainui.excelFilePath.setText(excel_file)
workbook = openpyxl.load_workbook(excel_file)
worksheet = workbook.active
self.mainui.excelTableOutput.setRowCount(worksheet.max_row)
self.mainui.excelTableOutput.setColumnCount(worksheet.max_column)
for i in range(worksheet.max_column):
self.mainui.excelTableOutput.setColumnWidth(i, 170)
self.mainui.excelTableOutput.horizontalHeader().setVisible(False)
listValues = list(worksheet.values)
self.mainui.excelStatusBar.setText("Loading Excel table")
worksheet.max_column))
rowIndex = 0
for row in listValues:
colIndex = 0
for column in row:
self.mainui.excelTableOutput.setItem(rowIndex, colIndex, QtWidgets.QTableWidgetItem(column))
colIndex += 1
rowIndex += 1
else:
self.mainui.excelFilePath.setText("Chose Excel file")
Thats work fine, but importing large tables takes a lot of time and the application freezes.
I decided to use multithreading and refactored code
class ExcelParser(QtCore.QObject):
load_table = QtCore.pyqtSignal(str, int, int, int, int)
@pyqtSlot(list, int, int)
def loadExcelTableInThread(self, listValues, maxRow, maxColumn):
rowIndex = 0
for row in listValues:
colIndex = 0
for column in row:
self.load_table.emit(column, rowIndex, colIndex, maxRow, maxColumn)
colIndex += 1
rowIndex += 1
class ZumParserWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.mainui = Ui_MainWindow()
self.mainui.setupUi(self)
# Creating worker thread
self.ExcelParser = ExcelParser()
self.ExcelThread = QtCore.QThread()
self.ExcelParser.load_table.connect(self.loadExcelTableInThread)
self.ExcelParser.moveToThread(self.ExcelThread)
self.ExcelThread.start()
# Get data from Excel file
def getExcelFile(self):
excel_file, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open file", downloads_path, "Excel files (*.xlsx)")
if excel_file != "":
self.mainui.excelFilePath.setText(excel_file)
workbook = openpyxl.load_workbook(excel_file)
worksheet = workbook.active
self.mainui.excelTableOutput.horizontalHeader().setVisible(False)
listValues = list(worksheet.values)
self.mainui.excelStatusBar.setText("Loading Excel table")
QtCore.QMetaObject.invokeMethod(self.ExcelParser, "loadExcelTableInThread",
QtCore.Qt.ConnectionType.QueuedConnection,
QtCore.Q_ARG(list, listValues),
QtCore.Q_ARG(int,worksheet.max_row),
QtCore.Q_ARG(int, worksheet.max_column))
else:
self.mainui.excelFilePath.setText("Chose Excel file")
def loadExcelTableInThread(self, column, rowIndex, colIndex, maxRow, maxColumn):
self.mainui.excelTableOutput.setRowCount(maxRow)
self.mainui.excelTableOutput.setColumnCount(maxColumn)
for i in range(maxColumn):
self.mainui.excelTableOutput.setColumnWidth(i, 170)
self.mainui.excelTableOutput.setItem(rowIndex, colIndex, QtWidgets.QTableWidgetItem(column))
This option works with some tables, but large files crash the application and in console i see
Process finished with exit code -1073740791 (0xC0000409)
I need multi-threaded loading to work with any Excel tables and not cause the application to crash. Otherwise, there is no point in it, since small tables do not cause freezes even without multithreading.
本文标签: pythonApp crash when im uploading big excel table using multithreadStack Overflow
版权声明:本文标题:python - App crash when im uploading big excel table using multithread - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741314872a2371854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论