1

I have a pyqt5 application. In the program you can open various other windows from the MainWindow. The different windows are stored in a separate module and are imported into the main file, which represents the MainWindow. Furthermore I use several custom widgets, which are also stored in a separate module and imported into the main file.

The program runs smoothly, but it takes a few seconds for the program to start. Exactly I can not say what causes the delay at startup. However, it seems to be due to my custom modules.

The main file looks something like his:

#other imports
...
#import custom modules
from MyWidgets import MyTreeView
from MyWindows import MySecondWindow

basedir = Path(__file__).parent.absolute()

class App(QMainWindow):

    def __init__(self):
        super(App, self).__init__()
        uic.loadUi(os.path.join(basedir, "gui", "MainWindow.ui"), self)

Is it possible to load the modules in a thread and after the loading is finished start the App, which uses the modules, that were loaded inside the thread.

from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication, QSplashScreen
from PyQt5.QtGui import QPixmap

class LoaderThread(QThread):
    loaded = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)

    def run(self):
        # loading of heavy modules here
        from MyWidgets import MyTreeView
        from MyWindows import MySecondWindow
        self.loaded.emit()

class SplashScreen(QSplashScreen):
    def __init__(self, parent=None):
        super().__init__(parent)

        # Set the splash screen image
        self.setPixmap(QPixmap('/Users/user/PycharmProjects/LR2/icons/home.png'))

        # Start loading the modules in a separate thread
        self.loader_thread = LoaderThread()
        self.loader_thread.loaded.connect(self.start_main_window)
        self.loader_thread.start()

    def start_main_window(self):
        self.close()
        self.main_window = App()
        self.main_window.show()


if __name__ == '__main__':
    app = QApplication([])
    splash = SplashScreen()
    splash.show()
    app.exec_()
Mazze
  • 383
  • 3
  • 13
  • If you open the window and then import the custom modules right in this order, the application could freeze it's UI. Be thankful the freezing happens before the windows are shown, otherwise it could be way worse (you would need to stick with threading, which, for example, [just complicates things](https://stackoverflow.com/questions/12389526/import-inside-of-a-python-thread) here). – Carl HR Feb 10 '23 at 17:06
  • I don't personally don't see the point of shortening the loading time, unless it's for developing and testing. For example: when you open a game on your PC, do you keep closing and reopening the game to see if it loads fast? No, you just open the game; waits for a few seconds (or minutes); play it until you're bored; and then closes it. That's what should happen for you own app as well. If the user wants to keep closing and reopening the application everytime, it's their own problem. – Carl HR Feb 10 '23 at 17:06
  • A solution can be found here: https://stackoverflow.com/questions/71688209/using-pyqt5-splash-screen-to-cover-module-load-time – Mazze May 01 '23 at 10:39

0 Answers0