如何将参数传递给QRqnable for pyqt5?

时间:2018-06-14 10:22:33

标签: multithreading pyqt5

我可以将函数传递给QRunnable但是我无法将函数参数与它们一起传递。

例如,在第19行,它将除外

  

worker = Worker(self.bitfinex)#参数已删除

但是当我传递参数

时会引发以下错误
  

worker = Worker(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)

由于

0 个答案:

没有答案