Search code examples
pythonpyqtdelay

Slight delay to display PyQt window contents in Python program


The following Python program when run(both in PyCharm or by double-clicking the .py in windows explorer), has a noticable delay(sometimes like 5s) before displaying the contents of the window. (The main window itself appears on run)

import os
from PyQt5 import QtCore, QtGui, QtWidgets, uic

class MyWindow(QtWidgets.QMainWindow):
    i = 0#global variable

    def __init__(self):
        super(MyWindow, self).__init__()
        uic.loadUi('hello_world.ui', self)
        self.pushButton.clicked.connect(self.onButtonClicked)

    def onButtonClicked(self):
        self.i+=1
        print('hello world! ' + str(self.i))


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

The UI file is saved using QtDesigner as a .ui file in the same directory as the .py script

UPDATE

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'hello_world.ui'
#
# Created by: PyQt5 UI code generator 5.13.2
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 500)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(140, 80, 186, 41))
        self.pushButton.setStyleSheet("QPushButton {\n"
"    background-color: red;\n"
"    border-style: outset;\n"
"    border-width: 2px;\n"
"    border-radius: 10px;\n"
"    border-color: beige;\n"
"    font: bold 14px;\n"
"    min-width: 10em;\n"
"    padding: 6px;\n"
"}\n"
"QPushButton:pressed {\n"
"    background-color: rgb(224, 0, 0);\n"
"    border-style: inset;\n"
"}")
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 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)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

The above updated was code directly generated by using pyuic. But still it shows a noticable delay. (First the main window is displayed with the title bar and it's completely white where the contents should be. After about a second, the contents are loaded)


Solution

  • I followed https://www.learnpyqt.com/courses/qt-creator/first-steps-qt-creator/ tutorials which were really good (probably the best one on PyQt5 out there available for free) & got my problem solved.

    For anybody having such a problem,

    Steps for correct PyQt5 application (at least worked for me),

    • Write code in VS Code (don't know whether has any real effect than using PyCharm)
    • Design the UI using QtCreator
    • Use pyuic5 to convert the generated .ui to .py
    • Import that .py into the main python script

    All these steps are clearly stated in the link above.