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_()