gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:53:24 33 4
gpt4 key购买 nike

我在处理线程的 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com