簡體   English   中英

使用QThread和Python裝飾器的問題

[英]Probleme with QThread and Python decorator

我的代碼有關於Qthread的問題,PyQt在Python 3.6上是錯誤的文件

F:\PROGRAMMES\Qt\Projet\cYrAnalyzer\worker2.py", line 14, in run
self._target(*self._args, **self._kwargs)
TypeError: execute_this_function() takes 1 positional argument but 2 were given
[Finished in 3.1s with exit code 3221226505]

這是類worker2.py:

from PyQt5.QtCore import *
from PyQt5 import QtCore
from functools import wraps

class Runner(QtCore.QThread):
    def __init__(self, target, *args, **kwargs):
        super().__init__()
        self._target = target
        self._args = args
        self._kwargs = kwargs

    def run(self):
        self._target(*self._args, **self._kwargs)


def run(func):
    @wraps(func)
    def async_func(*args, **kwargs):
         runner = Runner(func, *args, **kwargs)
         func.__runner = runner
         runner.start()
    return async_func

這是主要類:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import gui, os, time
from worker2 import *

class MyWindow(QMainWindow):

    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.ui = gui.Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.compteur.clicked.connect(self.execute_this_function)

    @run
    def execute_this_function(self):
        self.ui.edit_compteur.setText("1")
        time.sleep(0.5)
        self.ui.edit_compteur.setText("2")
        time.sleep(0.5)
        self.ui.edit_compteur.setText("3")
        time.sleep(0.5)
        self.ui.edit_compteur.setText("4")
        time.sleep(0.5)
        self.ui.edit_compteur.setText("5")
        time.sleep(0.5)

錯誤:

self._target(*self._args, **self._kwargs)
TypeError: execute_this_function() takes 1 positional argument but 2 were given

您指出的錯誤是由於clicked是一個重載信號引起的,也就是說實際上它們是2個信號,一個帶有布爾簽名而另一個沒有簽名: clicked = pyqtSignal([], [bool])並且默認情況下它會嘗試傳遞布爾值。 因此解決方案是使用@pyqtSlot建立插槽簽名

由於先前的錯誤而仍未顯示的另一個錯誤是您不應直接更新GUI。 一個簡單的策略是在functools.partial旁邊使用QTimer.singleShot(0, ...) ,如果你需要傳遞額外的參數。

import time
from PyQt5 import QtCore, QtWidgets
from functools import partial

from worker2 import run
import gui


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui = gui.Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.compteur.clicked.connect(self.execute_this_function)

    @run
    @QtCore.pyqtSlot()
    def execute_this_function(self):
        wrapper = partial(self.ui.edit_compteur.setText, "1")
        QtCore.QTimer.singleShot(0, wrapper)
        time.sleep(0.5)
        wrapper = partial(self.ui.edit_compteur.setText, "2")
        QtCore.QTimer.singleShot(0, wrapper)
        time.sleep(0.5)
        wrapper = partial(self.ui.edit_compteur.setText, "3")
        QtCore.QTimer.singleShot(0, wrapper)
        time.sleep(0.5)
        wrapper = partial(self.ui.edit_compteur.setText, "4")
        QtCore.QTimer.singleShot(0, wrapper)
        time.sleep(0.5)
        wrapper = partial(self.ui.edit_compteur.setText, "5")
        QtCore.QTimer.singleShot(0, wrapper)
        time.sleep(0.5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM