我认为您在使用装饰器方面过于复杂。您可以使用大约3-4行设置代码轻松地将代码包装在新线程中。另外,我不认为您应该直接从另一个线程调用完成的插槽。您应该使用连接的信号将其激活。
import sysfrom time import sleepfrom PyQt5.QtCore import *from PyQt5.QtWidgets import *class Signals(QObject): update = pyqtSignal(int) enable_button = pyqtSignal(bool)class Window(QWidget): def __init__(self, *args, **kwargs): QWidget.__init__(self, *args, **kwargs) self.button = QPushButton("Run", self) self.button.clicked.connect(self.onButton) self.progress = QProgressBar(self) self.progress.setTextVisible(False) self.layout = QVBoxLayout() self.layout.setContentsMargins(5, 5, 5, 5) self.layout.addWidget(self.button) self.layout.addWidget(self.progress) self.layout.addStretch() self.worker_thread = QThread() self.worker_thread.run = self.worker self.worker_thread.should_close = False self.signals = Signals() self.signals.update.connect(self.progress.setValue) self.signals.enable_button.connect(self.button.setEnabled) self.setLayout(self.layout) self.show() self.resize(self.size().width(), 0) # Override def closeEvent(self, e): self.worker_thread.should_close = True self.worker_thread.wait() @pyqtSlot() def onButton(self): self.button.setDisabled(True) self.worker_thread.start() # Worker thread, no direct GUI updates! def worker(self): for i in range(101): if self.worker_thread.should_close: break self.signals.update.emit(i) sleep(0.1) self.signals.enable_button.emit(True)app = QApplication(sys.argv)win = Window()sys.exit(app.exec_())


