Python:'PyQt5。QtCore.pyqt信号'对象没有属性'connect'

2024-10-01 07:20:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我在pyqtSignal通过线程时遇到问题。 我得到以下错误:

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

在命令中:

^{pr2}$

该程序基本上是一个用PyQt设计器设计的主窗口,通过线程调用一些不同的函数。 我想在主窗口代码中获取一些线程的完成信号(以便显示结果等)。下面是解释其体系结构的代码的一小部分。在

主代码:

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()

类计算开始计算:

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在文档中指定的类级别上定义。在

谢谢!在


Tags: 代码selfobjectdefconnectsome线程thread
2条回答

也许您需要从QtCore类继承?在

from PySide import QtCore
class CALCULS_AE(QtCore.QThread):

    #Signals
    Uni_finished = QtCore.pyqtSignal()
    Reb_finished = QtCore.pyqtSignal()

    ...

您有以下错误:

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

  • 如果要使用Python的本机threading,那么使用QThread是没有意义的,有两个元素做了同样的操作,因此将QThread更改为QObject

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

不正确

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

对吧

^{pr2}$
  • 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()

相关问题 更多 >