Python PyQt5信号和放大器插槽

时间:2013-10-17 13:23:29

标签: python python-3.x signals pyqt4 pyqt5

我在将新的pyqt5 signals and slots应用到脚本中时遇到了一些麻烦,其目的是测试/调用我一直试图解决的另一个问题,GUI冻结/崩溃......目标是这样的这些信号和插槽运行正常,GUI在运行+/- 30秒后不会崩溃,只是继续计算数字直到时间结束。我提供了一个pyqt4示例,尽管有一个pyqt5解决方案会很棒。谢谢:))

from time import sleep
import os
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtGui import * 
import random
import os
import time


class Cr(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)
    def run(self):

        while True:
            rndInt = random.randint(1, 100000)
            timesleep = random.random()
            time.sleep(timesleep)
            for i in range(120):
                self.emit(QtCore.SIGNAL('host_UP'), 'foo' + str(rndInt), i)
                QtGui.QApplication.processEvents()


class Main_Window(QWidget):

    def __init__(self, *args): 
        QWidget.__init__(self, *args)
        self.relativePath = os.path.dirname(sys.argv[0])

        self.Main_Window = uic.loadUi("Main_Window.ui", self)
        self.Main_Window.show()
        self.Main_Window.move(790, 300)

        self.GU = []
        ProgressThreads = self.printThreads
        self.details_label = []
        for i in range(120):
            self.details_label.insert(i, 0)
            self.details_label[i] = QLabel(" ")
            ProgressThreads.addWidget(self.details_label[i])
            ProgressThreads.addSpacing(6)
            self.details_label[i].setText(Tools.Trim.Short('Idle', 7))
            self.GU.insert(i, Cr())
            self.GU[i].start()

        self.connect(self.GU, QtCore.SIGNAL("host_UP"), self.UpdateHost)


    def UpdateHost(self, str1, pos1):
        self.details_label[pos1].setText(str1)


class guiUpdate():
    def GUI_main(self):
        self.GUI = GUI



if __name__ == "__main__": 
    app = QApplication(sys.argv)
    guiUpdate.GUI_main.GUI = Main_Window()
    sys.exit(app.exec_())

感谢您的帮助:)

更新 下面的脚本是上面脚本的希望正确的PyQt5版本。但是,崩溃和“未响应”消息的问题仍未解决

from time import sleep
import os
from PyQt5 import QtCore, QtGui, uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QObject, pyqtSignal
import random
import os
import time
import Tools
import sys


class Cr(QtCore.QThread):
    def __init__(self, sam):
        QtCore.QThread.__init__(self)
        self.sam = sam

    def run(self):

        while True:
            rndInt = random.randint(1, 100000)
            timesleep = random.random()
            time.sleep(timesleep)
            for i in range(5):
                #time.sleep(1)

                self.sam.connect_and_emit_trigger('foo' + str(rndInt), i)
                #self.emit(QtCore.SIGNAL('host_UP'), 'foo' + str(rndInt), i)
                #QtGui.QApplication.processEvents()


class Main_Window(QWidget):

    def __init__(self, *args): 
        QWidget.__init__(self, *args)
        self.relativePath = os.path.dirname(sys.argv[0])

        self.Main_Window = uic.loadUi("Main_Window.ui", self)
        self.Main_Window.show()
        self.Main_Window.move(790, 300)

        sam = Foo()


        self.GU = []
        ProgressThreads = self.ProgressThreads
        self.details_label = []
        for i in range(5):
            self.details_label.insert(i, 0)
            self.details_label[i] = QLabel(" ")
            ProgressThreads.addWidget(self.details_label[i])
            ProgressThreads.addSpacing(6)
            self.details_label[i].setText(Tools.Trim.Short('Idle', 7))
            self.GU.insert(i, Cr(sam))
            self.GU[i].start()


class Foo(QObject):
    # Define a new signal called 'trigger' that has no arguments.
    trigger = pyqtSignal()
    def connect_and_emit_trigger(self, str, i):
        self.str = str
        self.i = i

        self.trigger.connect(self.handle_trigger)
        self.trigger.emit()

    def handle_trigger(self):
        guiUpdate.GUI_main.GUI.details_label[self.i].setText(self.str)




class guiUpdate():
    def GUI_main(self):
        self.GUI = GUI



if __name__ == "__main__": 
    app = QApplication(sys.argv)
    guiUpdate.GUI_main.GUI = Main_Window()
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:0)

使用线程的新推荐方法(以及我获得最佳结果的方法)是使用moveToThread()而不是直接子类化QThread。简而言之:

  1. 编写一个QObject子类来完成实际工作(让我们称之为QMyWorker)。这可能看起来有点像您现有的qthread子类,使用start()run()方法等。

  2. 创建QMyWorker的无父实例

  3. 创建QThread的无父实例

  4. 使用QMyWorker.moveToThread(your_thread_instance)(我按内存检查,仔细检查doc中的API)。

  5. 致电您的QMyWorker.start()

  6. 这种方法适合我很长的工作(4GB文件等)。

答案 1 :(得分:0)

我使用QThreadPool,QRunnable与一个worker一起使用,每个线程可以使更多的worker。 很好的例子,这里有解释 https://martinfitzpatrick.name/article/multithreading-pyqt-applications-with-qthreadpool/

我的PYQT5也冻结了,现在我可以通过打印TimeStamp对其进行微调了