Reputation: 994
first i created the functional parts of my code and later decided to add a interface to it, so i have linked the interface and and the main function of the previous code as bellow.
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(921, 988)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.sheet2 = QtWidgets.QLabel(self.centralwidget)
self.sheet2.setObjectName("sheet2")
self.verticalLayout.addWidget(self.sheet2)
self.sheet1 = QtWidgets.QLabel(self.centralwidget)
self.sheet1.setObjectName("sheet1")
self.verticalLayout.addWidget(self.sheet1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 921, 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.label_2.setText(_translate("MainWindow", "TextLabel"))
# self.label.setText(_translate("MainWindow", "TextLabel"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
def update_sheet2(self, Image):
qim = ImageQt(Image)
pix = QtGui.QPixmap.fromImage(qim)
pix = pix.scaled(self.sheet2.width(), self.sheet2.height(), QtCore.Qt.KeepAspectRatio)
self.sheet2.setPixmap(pix)
self.sheet2.setAlignment(QtCore.Qt.AlignCenter)
def update_sheet1(self, Image):
qim = ImageQt(Image)
pix = QtGui.QPixmap.fromImage(qim)
pix = pix.scaled(self.sheet1.width(), self.sheet1.height(), QtCore.Qt.KeepAspectRatio)
self.sheet1.setPixmap(pix)
self.sheet1.setAlignment(QtCore.Qt.AlignCenter)
def run_ui():
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
w.background = main(w)
t = Process(target=w.background.process)
t.start()
sys.exit(app.exec_())
in the function named main(ui)
in this code, the codes where it uses the ui is as follows. and i used the run_ui()
function to run the code
def main(ui):
for i in range(100000000):
x=1
y = x*x*x
img = Image.open('XXX.png'.format(GRAY_PATH,1))
ui.update_sheet1(img)
if __name__ == '__main__':
run_ui()
and i have passed the ui 'w' as an argument to the main funciton, where it uses that reference to call the update_sheet1,2 functions with image data.
this lags the GUI and its always not responding and the images also do not appear on the GUI.
i think this has something to do with the way i liked the interface. but dont know how to fix it.
thanks for any help.
Upvotes: 1
Views: 102
Reputation: 243937
Qt does not support multiprocessing so to remove complexity from the problem use threading. In this case Qt also indicates that the GUI should not be modified from another thread, instead of that I create a QObject and export it to the other thread, this QObject has a signal that transports the image.
On the other hand when you do main(w) you are invoking the heavy task in the main process and that causes the GUI to freeze, instead you have to pass the name of the function, and the arguments of that function through of args:
from PyQt5 import QtCore, QtGui, QtWidgets
from PIL import Image
from PIL.ImageQt import ImageQt
from threading import Thread
# ...
class Signaller(QtCore.QObject):
imageSignal = QtCore.pyqtSignal(Image.Image)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
def update_sheet2(self, Image):
# ...
@QtCore.pyqtSlot(Image.Image)
def update_sheet1(self, Image):
qim = ImageQt(Image)
pix = QtGui.QPixmap.fromImage(qim)
pix = pix.scaled(self.sheet1.width(), self.sheet1.height(), QtCore.Qt.KeepAspectRatio)
self.sheet1.setPixmap(pix)
self.sheet1.setAlignment(QtCore.Qt.AlignCenter)
def run_ui():
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
signaller = Signaller()
signaller.imageSignal.connect(w.update_sheet1)
t = Thread(target=main, args=(signaller,), daemon=True)
t.start()
sys.exit(app.exec_())
def main(signaller):
for i in range(100000000):
x=1
y = x*x*x
img = Image.open('XXX.png')
signaller.imageSignal.emit(img)
if __name__ == '__main__':
run_ui()
Upvotes: 1