I'm having an issue with a pyqtSignal going through threads. I get the following error :
AttributeError: 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect'
on the command :
CALCULS_AE.Uni_finished.connect(self.getFinishThread())
The program is basically a mainwindow designed with PyQt Designer and calling a few different functions through threads. I want to get the finish signal of some threads in my MainWindow code (in order to show the results, etc...). Below is a little part of the code to explain its architecture.
Main Code :
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 which start the calculation :
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 are defined on class level as specified in the documentation.
Thanks !
You have the following errors:
you must create a Calculs object: self.calculs = Calculs()
If you are going to use the native threading of Python it does not make sense to use QThread, there are 2 elements that do the same so change the QThread to QObject.
When you connect a signal to a function you must pass the name of the function, not the evaluated function.
incorrect
[...].finished.connect(self.getFinishThread())
Right
[...].finished.connect(self.getFinishThread)
target expects the name of the function, not the evaluated function.
If you are not going to modify the constructor of the Calculs class, it is not necessary to implement it.
Code:
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With