0

I have this GUI that takes in file inputs and processes them. Naturally, this takes a fair amount of time and it would be nice for the user to see what is happening in the background as they wait for the file processing to finish. For example: "Creating dataframe...", "Exporting dataframe to csv...", "Done!". I have these types of statements placed in the middle of the function.

The problem: The statements get updated to the Log, but only when the entire function is done running.

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    # Initialize GUI
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)

        # Set the font in the logger
        font = QFont()
        font.setPointSize(9)
        self.Log_textEdit.setFont(font)

        # Allow selecting multiple files in QWidgetLists
        self.KeysightCurveTracer_List.setSelectionMode(QAbstractItemView.ExtendedSelection)

        # Drag-Drop Functionality
        self.setAcceptDrops(True)

        # setup user click handlers
        self.main_tab.tabBarClicked.connect(lambda: GUIMethods.setCurrentTab())
        self.KeysightCurveTracer_AddFile_Button.clicked.connect(lambda: GUIMethods.addFile())
        self.KeysightCurveTracer_AddFolder_Button.clicked.connect(lambda: GUIMethods.addFolder())
        self.KeysightCurveTracer_Remove_Button.clicked.connect(lambda: GUIMethods.removeFile())
        self.KeysightCurveTracer_Characterization_button.clicked.connect(lambda: GUIMethods.keysightCurveTracer_Characterization())
        self.KeysightCurveTracer_ModuleEOL_Button.clicked.connect(lambda: GUIMethods.keysightCurveTracer_ModuleEOL())
        self.KeysightCurveTracer_RemoveAll_Button.clicked.connect(lambda: GUIMethods.removeAllFiles())
        

# Start of program
if __name__ == "__main__":
    # "Starts" program in command prompt
    QCoreApplication.setLibraryPaths([os.getcwd() + "\\virtualenv\\Lib\\site-packages\\PyQt5\\Qt5\\plugins"])
    
    app = QtWidgets.QApplication(sys.argv)   
    app.setStyleSheet("QLabel{font-size: 10pt;}")
    app.setWindowIcon(QtGui.QIcon(os.path.join(os.path.dirname(__file__), 'Icon.ico')))

    # Initialize and show GUI
    window = MainWindow()
    window.show()

    # Passes the window to other python files
    GUIMethods.window = window
    FileProcessing.window = window
    KeysightCurveTracer.window = window

    GUIMethods.log("Initialized Post-Processing App")

    app.exec_()

Here's an example of my integration of logging:

GUIMethods.log("Finding index.txt")
        for fullPath in fullPathList:
            if "index.txt" in fullPath:
                GUIMethods.log("index.txt found")
                indexFile = fullPath
                break

And here is the actual logging function:

def log(string):
    if "ERROR" in type(string).__name__.upper():
        logString = type(string).__name__ + ": " + str(string)

    else:
        logString = string

    window.Log_textEdit.append(logString)

I have tried looking around, but I could not find anything that would help my certain case or perhaps I am not using my brain enough to figure out how to warp the answers for my case. I have tried some tutorials like this and this. I have also tried a couple answers from stackoverflow like here or here or here or here. This answer is the closest to my case though.

What I find hard with these answers is that they're initializing the QWidget from within the MainWindow init() where my QWidgets are being made in a separate script (because I'm using Qt Designer and use a batch file to update the file). And I'm not sure how to weave in my methods into the multithreading. Any help is appreciated!

  • Read *all* answers for the post marked duplicate, specifically those that explicitly address the threading issue (UI elements cannot be accessed from external threads). – musicamante Dec 08 '22 at 22:35

0 Answers0