pythonpython-3.xpyqtpyqt5pixmap

How to show a pixmap in pyqt5 in __init__


I have a simple Python application using PyQt5 that shall perform an update. Currently I'm stuck directly within the __init__, when I add two pixmaps and a lineEdit whose text that I want to update during a calculation.

Whenever I use the main_window.change() the GUI is not shown until the change() is finished.

The pictures are not shown.

Without the change method it's showing the pics

Without change method GUI is shown correctly

If I add a QMessageBox into the for loop, the message is shown of course, but also the updated GUI becomes visible.

The message box updates the GUI

Adding a self.update() did not help.

class AcselStarter(QtWidgets.QMainWindow, Ui_ACSEL_Starter):

    def __init__(self, parent=None):

        super(AcselStarter, self).__init__(parent)
        Ui_ACSEL_Starter.__init__(self)
        self.setupUi(self)

        pixmapAcsel = QPixmap('../fig/ACSEL.png')
        self.labelAcsel.setPixmap(pixmapAcsel)

        pixmapMubea = QPixmap('../fig/CAELogo_height60.png')
        self.labelMubea.setPixmap(pixmapMubea)

        self.lineEditProgress.setText(str(0.001))

    def change(self):
        for i in range(0, 100, 10):
            self.lineEditProgress.setText(str(i))
            # QtWidgets.QMessageBox.information(self, PROGRAMM_NAME, 'Replot', QtWidgets.QMessageBox.Ok)



if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    main_window = AcselStarter()
    main_window.show()
    time.sleep(5)
    main_window.change()

    sys.exit(app.exec_())

For completeness, here's my UI file from Qt Designer:

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

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_ACSEL_Starter(object):
    def setupUi(self, ACSEL_Starter):
        ACSEL_Starter.setObjectName("ACSEL_Starter")
        ACSEL_Starter.resize(320, 180)
        self.centralwidget = QtWidgets.QWidget(ACSEL_Starter)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.widgetPics = QtWidgets.QWidget(self.centralwidget)
        self.widgetPics.setObjectName("widgetPics")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widgetPics)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.labelAcsel = QtWidgets.QLabel(self.widgetPics)
        self.labelAcsel.setText("")
        self.labelAcsel.setObjectName("labelAcsel")
        self.horizontalLayout_2.addWidget(self.labelAcsel)
        self.labelMubea = QtWidgets.QLabel(self.widgetPics)
        self.labelMubea.setText("")
        self.labelMubea.setObjectName("labelMubea")
        self.horizontalLayout_2.addWidget(self.labelMubea)
        self.gridLayout.addWidget(self.widgetPics, 0, 0, 1, 1)
        self.widgetProgress = QtWidgets.QWidget(self.centralwidget)
        self.widgetProgress.setMaximumSize(QtCore.QSize(16777215, 30))
        self.widgetProgress.setObjectName("widgetProgress")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widgetProgress)
        self.horizontalLayout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize)
        self.horizontalLayout.setContentsMargins(0, 9, 0, 0)
        self.horizontalLayout.setSpacing(6)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.labelProgress = QtWidgets.QLabel(self.widgetProgress)
        self.labelProgress.setMaximumSize(QtCore.QSize(48, 16777215))
        self.labelProgress.setObjectName("labelProgress")
        self.horizontalLayout.addWidget(self.labelProgress)
        self.lineEditProgress = QtWidgets.QLineEdit(self.widgetProgress)
        self.lineEditProgress.setMaximumSize(QtCore.QSize(50, 16777215))
        self.lineEditProgress.setObjectName("lineEditProgress")
        self.horizontalLayout.addWidget(self.lineEditProgress)
        self.labelPercent = QtWidgets.QLabel(self.widgetProgress)
        self.labelPercent.setObjectName("labelPercent")
        self.horizontalLayout.addWidget(self.labelPercent)
        self.gridLayout.addWidget(self.widgetProgress, 1, 0, 1, 1)
        ACSEL_Starter.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(ACSEL_Starter)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 320, 21))
        self.menubar.setObjectName("menubar")
        ACSEL_Starter.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(ACSEL_Starter)
        self.statusbar.setObjectName("statusbar")
        ACSEL_Starter.setStatusBar(self.statusbar)

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

    def retranslateUi(self, ACSEL_Starter):
        _translate = QtCore.QCoreApplication.translate
        ACSEL_Starter.setWindowTitle(_translate("ACSEL_Starter", "ACSEL_Starter"))
        self.labelProgress.setText(_translate("ACSEL_Starter", "Progress"))
        self.labelPercent.setText(_translate("ACSEL_Starter", "%"))


I expect the GUI to be updated whenever I set new text to the lineEdit.

Thank you for your help in advance!


Solution

  • I just found out a solution by my own and used the nicer progress bar instead of the lineEdit. Here the solution when the progress bar is activated via code and not buttonClick. Because then a new thread has to be performed manually. The buttonClick does it automatically internally.

    class ThreadClass(QThread):
        valChanged = pyqtSignal(int)
    
        def run(self):
            print('run thread')  # e.g. file download
            count = 0
            while count < 100:
                count += 0.0005
                self.valChanged.emit(count)
            print('finished thread')
    
            main_window.my_property = True
            self.quit()
    
    
    class AcselStarter_Thread(QtWidgets.QMainWindow, Ui_ACSEL_Starter):
        """
        Activating the progress bar within the code without ButtonClick is not working.
        """
    
        def __init__(self, parent=None):
            super(AcselStarter_Thread, self).__init__(parent)
            Ui_ACSEL_Starter.__init__(self)
            self.setupUi(self)
    
            pixmapAcsel = QPixmap('../fig/ACSEL.png')
            self.labelAcsel.setPixmap(pixmapAcsel)
    
            pixmapMubea = QPixmap('../fig/CAELogo_height60.png')
            self.labelMubea.setPixmap(pixmapMubea)
    
            self.progressBar.setMaximum(100)
            self.progressBar.setValue(0)
    
            self.__my_property = False
    
        @property
        def my_property(self):
            return self.__my_property
    
        @my_property.setter
        def my_property(self, val):
            self.__my_property = val
            print('new val')
    
            if self.__my_property:
                self.do_something_after_progressbar()
    
        def update_progressbar(self, val):
            self.progressBar.setValue(val)
    
        def do_thread(self):
            # e.g. file update/download
            print('do_thread')
            self.threadClass = ThreadClass()
            self.threadClass.valChanged.connect(self.update_progressbar)
            self.threadClass.start()
    
        def do_something_after_progressbar(self):
            # e.g. moving the downloaded files and starting the updated app
            print('do_something_after_progressbar')
    
    
    if __name__ == '__main__':
        """
        Main method. Starts the application
        __name__ ==  '__main__' is true, if this file is run directly and not imported.
        """
    
        app = QtWidgets.QApplication(sys.argv)
    
        main_window = AcselStarter_Thread()
        main_window.show()
        main_window.do_thread()
    
        sys.exit(app.exec_())
    

    The output is the following, as expected/wanted:

    do_thread
    run thread
    finished thread
    new val
    do_something_after_progressbar