class Worker(QObject): mySignal = pyqtSignal() def __init__(self): super().__init__() def run(self): self.mySignal.emit() class MyRunnable(QRunnable): def __init__(self, func,*param): super().__init__() self.func = func self.param = param def run(self): self.func(self.param[0][0],self.param[0][1]) class test(QWidget): def __init__(self): super().__init__() self.name = 'name' self.setWindowTitle('测试') self.initUI() def initUI(self): self.label = QLabel(self) thread = QThread() worker = Worker() worker.moveToThread(thread) worker1 = Worker() worker1.moveToThread(thread) worker2 = Worker() worker2.moveToThread(thread) threadPool = QThreadPool() task1 = MyRunnable(self.func1, (1, 2)) task2 = MyRunnable(self.func2, (2, 2)) task3 = MyRunnable(self.func3, (3, 2)) #thread.started.connect(task1) worker.mySignal.connect(task1) worker1.mySignal.connect(task2) worker2.mySignal.connect(task3) threadPool.start(worker) threadPool.start(worker1) threadPool.start(worker2) threadPool.waitForDone() def func1(self,x,y): print(x+y) def func4(): self.label.setText(str(x+y)) func4() time.sleep(1) def func2(self, x,y): print(x + y) self.label.setText(str(x + y)) time.sleep(1) def func3(self, x,y): print(x + y) self.label.setText(str(x + y)) time.sleep(1) app = QApplication(sys.argv) main = test() main.show() sys.exit(app.exec_())报警:worker.mySignal.connect(task1) TypeError: argument 1 has unexpected type 'MyRunnable'怎么解决?
时间: 2024-01-14 09:02:04 浏览: 66
worker_thread_class.zip_c# Thread类_event class
在这段代码中,`worker.mySignal` 是一个信号,它发出的信号将会触发 `task1` 的运行。但是你在连接信号和槽时,使用了错误的语法。正确的语法是将信号连接到槽,而你却在将信号连接到了 `MyRunnable` 对象。为了解决这个问题,你需要将 `worker.mySignal` 连接到 `task1.run` 方法。修改代码如下:
```
worker.mySignal.connect(task1.run)
worker1.mySignal.connect(task2.run)
worker2.mySignal.connect(task3.run)
```
这样就可以将信号和槽正确地连接起来了。
阅读全文