首页 > 解决方案 > 如何将参数传递给 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)

谢谢

标签: multithreadingpyqt5

解决方案


推荐阅读