Python: 'PyQt5.QtCore.pyqtSignal' 对象没有属性 'connect'

标签 python multithreading pyqt5

我在处理线程的 pyqtSignal 时遇到问题。 我收到以下错误:

AttributeError: 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect'

在命令上:

 CALCULS_AE.Uni_finished.connect(self.getFinishThread())

该程序基本上是一个用 PyQt Designer 设计的主窗口,并通过线程调用几个不同的函数。 我想在我的 MainWindow 代码中获得一些线程的完成信号(为了显示结果等......)。下面是一小部分代码来解释它的架构。

主要代码:

class MainWindow(QMainWindow, Ui_MainWindow):

   def __init__(self):
       #Some code...
       self.Button.clicked.connect(self.launch_Calculation_clicked)

   def launch_Calculation(self):
       AE_Uni_thread = threading.Thread(target = CALCULS_AE.Calcul_AE_Uni, args = (arg1, arg2, arg3, arg4)) # Calculs_AE is a class defined in another file
       CALCULS_AE.Uni_finished.connect(self.getFinishThread()) # Another function doing some other stuff with the thread's results
       AE_Uni_thread.start()

开始计算的 CALCULS_AE 类:

class CALCULS_AE(object):
 #Signals
    Uni_finished = QtCore.pyqtSignal()
    Reb_finished = QtCore.pyqtSignal()

    def __init__(self):
        # Some Code

    def Calculs_AE_Uni(self, arg1, arg2, arg3, arg4):
        # Some Code launching the calculation
        self.Uni_finished.emit()

PS:pyqtSignals 是在文档中指定的类级别上定义的。

谢谢!

最佳答案

您有以下错误:

  • 您必须创建一个 Calculs 对象:self.calculs = Calculs()

  • 如果您打算使用 Python 的原生 threading,那么使用 QThread 没有意义,有 2 个元素做同样的事情,所以改变QThreadQObject

  • 将信号连接到函数时,必须传递函数的名称,而不是计算后的函数。

不正确

[...].finished.connect(self.getFinishThread())

[...].finished.connect(self.getFinishThread)
  • target 需要函数的名称,而不是求值函数。

  • 如果您不打算修改Calculs 类的构造函数,则没有必要实现它。

代码:

class Test(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        Ui_MainWindow.__init__(self)
        self.setupUi(self)      
        self.pushButton.clicked.connect(self.Launch_Test)

    def Launch_Test(self):
        self.calculs = Calculs()
        self.calculs.finished.connect(self.getFinishThread)
        test_thread = threading.Thread(target = self.calculs.Calcul_Test)
        test_thread.start()

    def getFinishThread(self):
        print('Good ! \n')
        #os.system('pause')

class Calculs(QObject):
    finished = pyqtSignal()

    def Calcul_Test(self):
        print('Test calcul\n')
        self.finished.emit()

关于Python: 'PyQt5.QtCore.pyqtSignal' 对象没有属性 'connect',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50859934/

相关文章:

python - PsyNet 实验抛出与 requirements.txt 相关的错误

Linux:信号对多线程的影响

multithreading - 从创建 UI 的同一线程更新 VCL。为什么?

Java线程池求解单个结果

python - 在某些情况下让 python argparse 仅使用部分默认值

python - 如何在pypy沙箱中导入numpy

python - plt.hist() vs np.histogram() - 意想不到的结果

python - PyQt5 停止从不同类添加的小部件内的计时器

python - 无法从 QVBoxLayout 中删除边距

qt - PySide2 QThread 错误 : QThread: Destroyed while thread is still running