multithreading - 如何将参数传递给 pyqt5 的 QRunnable?
问题描述
我能够将函数传递给 QRunnable,但是我无法将函数参数与它们一起传递。
例如在第 19 行,它将除了
worker = Worker(self.bitfinex) # 参数被移除
但是当我传递参数时会引发以下错误
工人=工人(self.bitfinex(self.ping))
完整的错误信息
Traceback (most recent call last):
File "C:\Users\carl-\Documents\GitHub\DTL-Template\threads.py", line 37, in run
self.fn(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable
真的在寻找一个答案,如果可能的话,我可以让事情保持原样,但如果可能的话,当然可以让我将参数直接传递到我的工作函数中?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import time
import traceback, sys
class MainWindow(QDialog):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.threadpool = QThreadPool()
self.ping = 'value'
button = QPushButton(self)
button.setText('call worker')
button.pressed.connect(self.api)
self.show()
def api(self):
# error!
# it will except self.bitfinex with no parameters
# however if I add parameters I get the error
worker = Worker(self.bitfinex(self.ping))
self.threadpool.start(worker)
def bitfinex(self, ping):
print(ping)
app = QApplication([])
window = MainWindow()
app.exec_()
工人
class Worker(QRunnable):
def __init__(self, fn, *args, **kwargs):
super(Worker, self).__init__()
# Store constructor arguments (re-used for processing)
self.fn = fn
self.args = args
self.kwargs = kwargs
@pyqtSlot()
def run(self):
self.fn(*self.args, **self.kwargs)
谢谢
解决方案
推荐阅读
- neural-network - CNTK 发送有关“sequence.gather”运算符的错误
- model-view-controller - ASP.NET MVC 网站路线图不起作用
- php - Laravel ModelNotFoundException:没有模型[]的查询结果
- flutter - 限制flutter中的整体缓存大小
- excel - 如何使用树视图将文件从 Outlook 拖放到 Excel 用户窗体?
- xamarin.forms - 使用 .HeightRequest 后如何检测 SizeChanged 事件
- reactjs - Jest Unexpected Identifier 需要
- c++ - 复制构造函数不是深度复制,返回错误,数组为空
- php - 如何使 rmdir() 不留下“无法访问”的文件夹?
- r - 如何更改R中的多个列值?