[英]How to structure large pyqt5 GUI without subclassing QThread and using QPushButtons to do long-running tasks
I'm looking at creating a program with a PyQt5 GUI.我正在考虑使用 PyQt5 GUI 创建程序。 The program will start with a UI with numerous buttons.该程序将从带有许多按钮的 UI 开始。 These buttons will be used to open other programs/completed long running tasks.这些按钮将用于打开其他程序/完成的长时间运行的任务。 I know I need to use QThread, but I am unsure how to structure the programs so that it scales properly.我知道我需要使用 QThread,但我不确定如何构建程序以使其正确扩展。
I've been at this for ages and have read numerous posts/tutorials.我已经在这里工作了很长时间,并且阅读了许多帖子/教程。 Most lean down the subclassing route.大多数人倾向于子类化路线。 In the past, I have managed to create a working program subclassing QThread, but I have since read that this metholodogy is not preferred.过去,我设法创建了一个子类化 QThread 的工作程序,但后来我读到这种方法不是首选。
I have a feeling I should be creating a generic worker and passing in a function with *args and **kwargs, but that is not in my skillset yet.我有一种感觉,我应该创建一个通用工作者,并传入带有 *args 和 **kwargs 的 function,但这还不是我的技能。
I originally created a thread for each button during the GUI init, but that seemed like it was going to get out of hand quickly.我最初在 GUI 初始化期间为每个按钮创建了一个线程,但这似乎很快就会失控。
I am currently at the stage of creating a thread under the slot connected to the button.clicked signal.我目前正处于连接到 button.clicked 信号的插槽下创建线程的阶段。 I am not sure if I then have to have a worker for each button or if I can/should make a generic worker and pass in a function.我不确定我是否必须为每个按钮配备一个工人,或者我是否可以/应该制作一个通用工人并传入 function。 Note: I have tried to do this but have not been able to do it.注意:我尝试过这样做,但无法做到。
#Import standard modules
import sys
#Import third-party modles
from PyQt5.QtCore import QObject, QThread, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QVBoxLayout, QWidget
class Worker(QObject):
#Custom signals?? or built-in QThread signals?
started = pyqtSignal()
finished = pyqtSignal()
def __init__(self):
super().__init__()
self.started.emit()
@pyqtSlot()
def do_something(self):
for _ in range(3):
print('Threading...')
QThread.sleep(1)
self.finished.emit()
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.initUi()
def initUi(self):
#Create GUI
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget )
self.vertical_layout = QVBoxLayout(self.centralWidget)
self.setWindowTitle('QThread Test')
self.setGeometry(300, 300, 300, 50)
self.button1=QPushButton("Task 1", self, clicked=self._task1_clicked)
self.button2=QPushButton("Task 2", self, clicked=self._task2_clicked)
self.vertical_layout.addWidget(self.button1)
self.vertical_layout.addWidget(self.button2)
self.vertical_layout.addStretch()
def _task1_clicked(self):
print('task1 clicked')
#Create the worker
self.my_worker = Worker()
#Create thread; needs to be done before connecting signals/slots
self.task1_thread = QThread()
#Move the worker to the thread
self.my_worker.moveToThread(self.task1_thread)
#Connect worker and thread signals to slots
self.task1_thread.started.connect(self._thread_started)
self.task1_thread.started.connect(self.my_worker.do_something)
self.my_worker.finished.connect(self._thread_finished)
#Start thread
self.task1_thread.start()
def _task2_clicked(self):
print('task2 clicked')
def _thread_started(self):
print('thread started')
def _thread_finished(self):
print('thread finished')
self.my_worker.isRunning = False
self.task1_thread.quit()
self.task1_thread.wait()
print('The thread is running: ' + str(self.task1_thread.isRunning()))
if __name__ == '__main__':
app = QApplication(sys.argv)
form = Window()
form.show()
app.exec_()
The above seems to work, but I feel like I have stumbled on to it and it is not the correct way of doing this.以上似乎可行,但我觉得我偶然发现了它,这不是正确的方法。 I do not want this to be my 'go-to' method if it is completely wrong.如果完全错误,我不希望这成为我的“首选”方法。 I'd like to be able to generate more complicated (more buttons doing things) programs compared to a one button/one task program.与一个按钮/一个任务程序相比,我希望能够生成更复杂(更多按钮做事)的程序。
In addition, I can't seem to get the QThread started and finished signals to fire without basically making them custom built signals.此外,如果基本上不制作定制信号,我似乎无法让 QThread 启动和完成信号触发。 This is one reason I think I am going about this wrong.这是我认为我做错了的原因之一。
from PyQt5 import QtCore
class AsyncTask(QtCore.QThread):
taskDone = QtCore.pyqtSignal(dict)
def __init__(self, *, task, callback=None, parent = None):
super().__init__(parent)
self.task = task
if callback != None:
self.taskDone.connect(callback)
if callback == None:
callback = self.callback
self.start()
def run(self):
try:
result = self.task()
print(result)
self.taskDone.emit(result)
except Exception as ex:
print(ex)
def callback(self):
print('callback')
Please try code above, call like this: AsyncTask(task=yourTaskFunction, callback=yourCallbackFunction)请尝试上面的代码,这样调用:AsyncTask(task=yourTaskFunction, callback=yourCallbackFunction)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.