0

I'm trying to make a GUI in PyQt5 that enable the user to select an Excel file to it.

After selecting the file, I want a TextEdit to change the text to path of the selected file.

My code is:

def carrega_arquivo(self, validador):

    app2 = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    janela = Orca_Obras()
    janela.Janela(Dialog)

    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog

    if validador == 'AutoCad':
        try:
            arquivo_autocad, _ = QtWidgets.QFileDialog.getOpenFileName(Dialog, 'Selecione a Planilha', '' ,'Arquivo Excel (*.xlsx *.xls);; Todos os Arquivos (*)', options=options)

            pasta = xlrd.open_workbook(arquivo_autocad)
            planilha = pasta.sheet_by_index(0)

            lista = [[planilha.cell_value(r, c) for c in range(planilha.ncols)] for r in range(planilha.nrows)]

            contador = 0
            cont_linha = 0
            cont_un = 0
            indice_codigo = []
            self.materiais_lst = []
            self.un_const_lst = []

            for dados in lista:
                if contador == 0:
                    for i, valor in enumerate(dados):
                        if 'APARTADO' in valor:
                            indice_apartado = i
                        if 'CÓDIGO' in valor:
                            indice_codigo.append(i)
                        if 'Count' in valor:
                            indice_count = i
                        if 'FIXAÇÃO' in valor:
                            indice_codigo.append(i)
                contador = 1
                indice_codigo = sorted(indice_codigo)

                if dados[0] != 'Count':
                    for indice in indice_codigo:
                        try:
                            dados[indice] = int(dados[indice])
                            self.materiais_lst.append([])
                            self.materiais_lst[cont_linha].append(int(dados[indice_apartado]))
                            self.materiais_lst[cont_linha].append(dados[indice])
                            self.materiais_lst[cont_linha].append(int(dados[indice_count]))
                            cont_linha += 1
                        except:
                            if len(dados[indice]) > 0:
                                self.un_const_lst.append([])
                                self.un_const_lst[cont_un].append(int(dados[indice_apartado]))
                                self.un_const_lst[cont_un].append(dados[indice])
                                self.un_const_lst[cont_un].append(int(dados[indice_count]))
                                cont_un += 1

            self.textEdit.setText(arquivo_autocad)
            self.label_23.setText(str(len(self.materiais_lst)) + ': Materiais')
            self.label_25.setText(str(len(self.un_const_lst)) + ': Unidade Construtiva')

        except:
            pass

After running the program and selecting the .xlsx file, the program still loading something and don't update the label or textEdit.

If I add the QtWidgets.QApplication.processEvents() after the last line before the except, the label and textEdit updates, but the program crashes showing the "Python Stops Work".

If I try to debug with a breakpoint on that new line (QtWidgets.QApplication.processEvents()) everything works fine and the texts updates normaly and the program don't keep "running" any background process.

But I can't use the debug because I want to make this program executable with PyInstaller after it's fully completed.

EDIT: minimal reproducible example:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog

class Orca_Obras(object):

    def setupUi(self, MainWindow):

        MainWindow.setObjectName('Orça-Obras')
        MainWindow.resize(300, 70)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName('centalwidget')

        self.lineEdit = QtWidgets.QLineEdit(MainWindow)
        self.lineEdit.setGeometry(QtCore.QRect(120, 10, 113,20))
        self.lineEdit.setObjectName('lineEdit')

        self.pushButton = QtWidgets.QPushButton(MainWindow)
        self.pushButton.setGeometry(QtCore.QRect(10, 10, 100, 23))
        self.pushButton.setObjectName('pushButton')
        self.pushButton.clicked.connect(self.carrega_autocad)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):

        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate('MainWindow', 'Orça-Obras'))
        self.pushButton.setText(_translate('MainWindow', 'Carrega Arquivo'))

    def janela(self, Dialog):
        self.Dialog = Dialog
        Dialog.setObjectName('Dialog')
        Dialog.resize(1, 1)

        self.traduzDialog(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def traduzDialog(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))

    def carrega_autocad(self):
        app2 = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        main = Orca_Obras()
        main.janela(Dialog)

        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog

        arquivo_autocad, _ = QtWidgets.QFileDialog.getOpenFileName(Dialog, 'Selecione a Planilha', '', 'Arquivo Excel (*.xlsx *.xls);; Todos os Arquivos (*)', options=options)
        self.lineEdit.setText(arquivo_autocad)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Orca_Obras()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

if I run the program and select the file, it crashes, even if I debug it. But if I put a breakpoint on line "self.lineEdit.setText(arquivo_autocad)", it works fine.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
DSCintra
  • 23
  • 4

0 Answers0