python – I am trying to load the homescreen after the splashscreen

I am trying to load my homescreen after the splashscreen. Upon running the code, the splashscreen manages to load but it stops there. How do I fix this?

This is my SplashScreen class

class SplashScreen(QWidget):
    def __init__(self):
        super().__init__()
        self.setFixedSize(700, 350)
        self.setWindowFlag(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.counter = 0
        self.n = 100
        self.initUI()
        self.timer = QTimer()
        self.timer.timeout.connect(self.loading)
        self.timer.start(30)

    def initUI(self):
        # layout to display splash scrren frame
        layout = QVBoxLayout()
        self.setLayout(layout)
        # splash screen frame
        self.frame = QFrame()
        layout.addWidget(self.frame)
        # splash screen title
        self.title_label = QLabel(self.frame)
        self.title_label.setObjectName('title_label')
        self.title_label.resize(690, 120)
        self.title_label.move(0, 5)  # x, y
        self.title_label.setText('Splash Screen')
        self.title_label.setAlignment(Qt.AlignCenter)
        # splash screen title description
        self.description_label = QLabel(self.frame)
        self.description_label.resize(690, 40)
        self.description_label.move(0, self.title_label.height())
        self.description_label.setObjectName('desc_label')
        self.description_label.setText('<b>Splash Screen PyQt-5</b>')
        self.description_label.setAlignment(Qt.AlignCenter)
        # splash screen pogressbar
        self.progressBar = QProgressBar(self.frame)
        self.progressBar.resize(self.width() - 200 - 10, 50)
        self.progressBar.move(100, 180)  # self.description_label.y()+130
        self.progressBar.setAlignment(Qt.AlignCenter)
        self.progressBar.setFormat('%p%')
        self.progressBar.setTextVisible(True)
        self.progressBar.setRange(0, self.n)
        self.progressBar.setValue(20)
        # spash screen loading label
        self.loading_label = QLabel(self.frame)
        self.loading_label.resize(self.width() - 10, 50)
        self.loading_label.move(0, self.progressBar.y() + 70)
        self.loading_label.setObjectName('loading_label')
        self.loading_label.setAlignment(Qt.AlignCenter)
        self.loading_label.setText('Loading...')

This is the function that lets the splashscreen load and launch the WindowApp class but it does not work. What I mean by does not work I meant that it does not load the WindowApp. It just closes.

    def loading(self):
        # set progressbar value
        self.progressBar.setValue(self.counter)
        # stop progress if counter
        # is greater than n and
        # display main window app
        if self.counter >= self.n:
            self.timer.stop()
            self.close()
            MainWindow = QtWidgets.QMainWindow()
            ui = WindowApp()
            ui.setupUi(MainWindow)
            MainWindow.show()

        self.counter += 1

This is my main window

class WindowApp(QMainWindow):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1006, 654)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(10, 10, 981, 641))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(10)
        self.label.setFont(font)
        self.label.setText("")
        self.label.setPixmap(QtGui.QPixmap("../pygui/Homescreen.png"))
        self.label.setScaledContents(True)
        self.label.setWordWrap(False)
        self.label.setObjectName("label")

        self.infoBtn = QtWidgets.QPushButton(self.centralwidget)
        self.infoBtn.setGeometry(QtCore.QRect(120, 550, 181, 41))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(15)
        self.infoBtn.setFont(font)
        self.infoBtn.setStyleSheet("border-radius: 10px;n"
                                   "border-color: 2px solid gray;n"
                                   "background-color: rgb(255, 255, 255);")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("icons/info_icon.png"),
                       QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.infoBtn.setIcon(icon)
        self.infoBtn.setObjectName("infoBtn")

        self.startBtn = QtWidgets.QPushButton(self.centralwidget)
        self.startBtn.setGeometry(QtCore.QRect(310, 270, 431, 111))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(28)
        self.startBtn.setFont(font)
        self.startBtn.setStyleSheet("background-color: #305E6C;n"
                                    "color: white;n"
                                    "border-radius: 10px;n"
                                    "border: 2px solid white")
        self.startBtn.setObjectName("startBtn")

        self.contactsBtn = QtWidgets.QPushButton(self.centralwidget)
        self.contactsBtn.setGeometry(QtCore.QRect(720, 30, 101, 101))
        self.contactsBtn.setStyleSheet("n"
                                       "background-color: #121212;n"
                                       "border-radius: 50%;n"
                                       "border: 2px solid white;")
        self.contactsBtn.setText("")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("icons/typcn_contacts.png"),
                        QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.contactsBtn.setIcon(icon1)
        self.contactsBtn.setIconSize(QtCore.QSize(40, 40))
        self.contactsBtn.setObjectName("contactsBtn")

        self.dateBtn = QtWidgets.QPushButton(self.centralwidget)
        self.dateBtn.setGeometry(QtCore.QRect(840, 30, 101, 101))
        self.dateBtn.setStyleSheet("n"
                                   "background-color: #121212;n"
                                   "border-radius: 50%;n"
                                   "border: 2px solid white;")
        self.dateBtn.setText("")
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("icons/Group.png"),
                        QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.dateBtn.setIcon(icon2)
        self.dateBtn.setIconSize(QtCore.QSize(40, 40))
        self.dateBtn.setObjectName("dateBtn")

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1006, 21))
        self.menubar.setObjectName("menubar")

        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

        self.infoBtn.clicked.connect(partial(self.show_popup))
        self.dateBtn.clicked.connect(self.openDateWindow)
        self.startBtn.clicked.connect(self.openDriveWindow)
        self.contactsBtn.clicked.connect(self.openContactWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.infoBtn.setText(_translate("MainWindow", "About"))
        self.startBtn.setText(_translate("MainWindow", "Driving Mode"))

    def show_popup(self):
        msg = QMessageBox()
        msg.setWindowTitle('About Us')
        msg.setText('Testing')
        msg.setIcon(QMessageBox.Information)
        x = msg.exec_()

    def openDateWindow(self, MainWindow):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_DateWindow()
        self.ui.setupUi(self.window, MainWindow)
        self.window.show()
        MainWindow.hide()

    def openDriveWindow(self, MainWindow):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_DriveWindow()
        self.ui.setupUi(self.window, MainWindow)
        self.window.show()
        MainWindow.hide()

    def openContactWindow(self, MainWindow):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_ContactWindow()
        self.ui.setupUi(self.window, MainWindow)
        self.window.show()
        MainWindow.hide()

I have tried changing this to WindowApp() just to check if the MainWindow has any problems but it manages to load without the splashscreen.

app = QApplication(sys.argv)
splash = SplashScreen()
splash.show()

sys.exit(app.exec_())

Leave a Comment