简体   繁体   English

将gui的功能分离到过程中。 滞后的

[英]separate functions from gui into process. lagging

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. 在此代码中名为main(ui)的函数中,使用ui的代码如下。 and i used the run_ui() function to run the code 我用run_ui()函数运行代码

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. 我已经将ui'w'作为参数传递给主函数,在函数中它使用该引用来调用带有图像数据的update_sheet1,2函数。

this lags the GUI and its always not responding and the images also do not appear on the GUI. 这会滞后GUI并使其始终不响应,并且图像也不会出现在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. 谢谢你的帮助。

Qt does not support multiprocessing so to remove complexity from the problem use threading. Qt不支持多处理,因此使用线程可以消除问题的复杂性。 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. 在这种情况下,Qt还指示不应从另一个线程修改GUI,而不是我创建一个QObject并将其导出到另一个线程,此QObject具有传输图像的信号。

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: 另一方面,当您执行main(w)时,您要在主进程中调用繁重的任务,这会导致GUI冻结,而您必须通过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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python在单独的过程中运行。 我可以统一包装器功能吗 - Python functions in a separate process. Can i unify the wrapper functions 在不滞后主进程的同时更新单独进程的线程 - Threading to update separate process while not lagging main process 如何在PySide中将功能与gui类分开? - How do I separate the functions from the gui class in PySide? GUI 在单独的进程中触发操作 - GUI triggering actions in a separate process Tkinter GUI通过具有功能的单独Python文件实现 - Tkinter GUI implemented with Separate Python file with functions Sphinx“无法创建进程”。 - Sphinx 'failed to create process.' 在这个 Python 进程中找不到 `java` 命令。 请确保已安装 Java 并为 `java` 设置了 PATH - `java` command is not found from this Python process. Please ensure Java is installed and PATH is set for `java` 将Pyqt GUI主应用程序作为单独的非阻塞进程运行 - Run Pyqt GUI main app as a separate, non-blocking process Tkinter GUI 能够更改单独连续过程的变量 - Tkinter GUI to be able to change variables of an separate continuous process Windows7中出现alembic“无法创建进程”的情况 - alembic “failed to create process.” in Windows7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM