0

There is a main window with a button that opens a second window. The second window starts a thread that processes an excel file (pandas or openpyxl) so a user can still interact with GUI when the excel is processed. When this processing is reached, the second window crashes (the main window still runs). When the second window is opened directly (as only component of the app), it works fine. It also works when no pandas or openpyxl is used.

The code follows:

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self) -> None:
        super().__init__()

        self.main_window_ui = uic.loadUi("ui/MainWindow.ui", self)
        self.main_window_ui.setWindowTitle("Test Results Application")
    
        self.__load_buttons()
    
        self.show()

    def __load_buttons(self):
        self.generate_test_report_button = self.main_window_ui.findChild(QPushButton, "testReportsButton")
        self.generate_test_report_button.clicked.connect(lambda: create_test_report.CreateTestReport())

class CreateTestReport(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.test_report_generator_ui = uic.loadUi("ui/TestReportGenerator.ui", self)
        self.test_report_generator_ui.setWindowTitle("Test report generator")

        self.__load_buttons()

    def __load_buttons(self):
        self.generate_button = self.test_report_generator_ui.findChild(QPushButton, "generateButton")
        self.generate_button.clicked.connect(self._generate_test_report)

    def _generate_test_report(self):

        params = [self.test_spec_location, self.test_report_destination, self.brand]
        self.thread = ResultsParserThread(params)
        self.thread.finished.connect(self.reportProgress)
        self.thread.start()


class ResultsParserThread(QThread):
    finished = pyqtSignal()

    def __init__(self, params):
        super().__init__()
        self.params = params

    def run(self):
        self.data = test_report_creation_2.ResultsParser(*self.params)
        self.data.requirement_tests()
        self.finished.emit()

class ResultsParser:
    def requirement_tests(self):
        for excel_file in self.test_groups[0]:
            excel = openpyxl.reader.excel.load_workbook(excel_file)
            print("here it crashes")
            for sheetname in excel.sheetnames:
                print(sheetname)
  • QThread already has a native signal called 'finished', so creating one for yourself with the same name might mess things up. – mahkitah Apr 28 '23 at 13:41
  • It's ok, it doesn't have any effect when it works without excel processing. – Dominik Filyo Apr 28 '23 at 13:54
  • 2
    @DominikFilyo That's not the point, you should not overwrite an existing signal, especially if it does the same thing. That said, the window does not "crash", it just gets deleted because it doesn't have a persistent reference. Don't connect to a lambda that just creates the instance, connect it to a function that creates it and make a persistent reference for it by making it an instance attribute (eg. `self.reportWin = CreateTestReport()`). Also, creating `self.generate_test_report_button` is pointless, as loadUi already creates references to widgets for `self`: `self.generateButton`. – musicamante Apr 28 '23 at 14:32
  • @musicamante Thank you, that works. Anyway, I still don't understand how it is related to pandas and why it worked without it. – Dominik Filyo May 02 '23 at 08:45
  • @DominikFilyo That has nothing to do with pandas, you probably did something else differently, possibly by adding a parent or using a reference that made the window persistent. – musicamante May 02 '23 at 11:58

0 Answers0