每点击一次按钮,弹出一个对话框(子窗口),同时开启一个子线程来执行任务并更新对话框内容,关闭对话框则关闭对应子线程
1. 建立一个简单的主界面和一个自定义对话框
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(327, 303) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) self.gridLayout.setObjectName("gridLayout") spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout.addItem(spacerItem, 0, 0, 1, 1) self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setObjectName("pushButton") self.gridLayout.addWidget(self.pushButton, 0, 1, 1, 1) spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout.addItem(spacerItem1, 0, 2, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 327, 23)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) self.pushButton.clicked.connect(MainWindow.open_dialog) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "多线程弹窗")) class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(369, 128) self.gridLayout = QtWidgets.QGridLayout(Dialog) self.gridLayout.setObjectName("gridLayout") self.buttonBox = QtWidgets.QDialogButtonBox(Dialog) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok) self.buttonBox.setObjectName("buttonBox") self.gridLayout.addWidget(self.buttonBox, 1, 0, 1, 1) self.progressBar = QtWidgets.QProgressBar(Dialog) self.progressBar.setProperty("value", 24) self.progressBar.setObjectName("progressBar") self.gridLayout.addWidget(self.progressBar, 0, 0, 1, 1) self.retranslateUi(Dialog) self.buttonBox.accepted.connect(Dialog.accept) self.buttonBox.rejected.connect(Dialog.reject) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
2. 每点击一次按钮,打开一个弹窗
class DialogWindow(QDialog, Ui_Dialog): def __init__(self, parent=None): super(DialogWindow, self).__init__(parent) self.setupUi(self) class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) def open_dialog(self): dialog = DialogWindow(self) dialog.show() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() sys.exit(app.exec_())
3. 打开弹窗的同时,打开一个子线程,更新对话框中的进度条
在子线程定义信号,关联对话框更新进度条的槽函数
class DialogWindow(QDialog, Ui_Dialog): def __init__(self, parent=None): super(DialogWindow, self).__init__(parent) self.setupUi(self) def update_progressbar(self, p_int): self.progressBar.setValue(p_int) # 更新进度条 class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.count = 0 def open_dialog(self): dialog = DialogWindow(self) dialog.show() self.thread = RunThread(self.count) self.count += 1 self.thread.update_pb.connect(dialog.update_progressbar) # 关联 self.thread.start() class RunThread(QThread): update_pb = pyqtSignal(int) # 定义更新进度条的信号 def __init__(self, count): super().__init__() self.count = count def run(self): for i in range(100): print('thread%s' % self.count, i, QThread().currentThreadId()) self.update_pb.emit(i) time.sleep(1) pass if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() sys.exit(app.exec_())
4. 关闭对话框,则关闭对应子线程
在对话框中添加自定义信号,并重写关闭事件,在关闭窗口时发送关闭子线程的信号
class DialogWindow(QDialog, Ui_Dialog): stop_thread = pyqtSignal() # 定义关闭子线程的信号 def __init__(self, parent=None): super(DialogWindow, self).__init__(parent) self.setupUi(self) def update_progressbar(self, p_int): self.progressBar.setValue(p_int) def closeEvent(self, event): self.stop_thread.emit() pass class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.count = 0 def open_dialog(self): dialog = DialogWindow(self) dialog.show() self.thread = RunThread(self.count) self.count += 1 self.thread.update_pb.connect(dialog.update_progressbar) dialog.stop_thread.connect(self.thread.terminate) self.thread.start() class RunThread(QThread): update_pb = pyqtSignal(int) def __init__(self, count): super().__init__() self.count = count def run(self): for i in range(1, 101): print('thread_%s' % self.count, i, QThread().currentThreadId()) self.update_pb.emit(i) time.sleep(1) pass if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() sys.exit(app.exec_())
5. 使用线程池QThreadPool管理子线程
使用QThreadPool, 线程需要继承QRunnable,而QRunnable只是namespace,没有继承QT的信号机制,
所以需要另外继承QObject来使用信号,我这里直接在线程中使用封装的信号向外部传递信息
class DialogWindow(QDialog, Ui_Dialog): stop_thread = pyqtSignal() # 定义关闭子线程的信号 def __init__(self, parent=None): super(DialogWindow, self).__init__(parent) self.setupUi(self) def update_progressbar(self, p_int): self.progressBar.setValue(p_int) def closeEvent(self, event): self.stop_thread.emit() pass class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.count = 0 self.pool = QThreadPool() self.pool.globalInstance() self.pool.setMaxThreadCount(10) # 设置最大线程数 def open_dialog(self): dialog = DialogWindow(self) dialog.show() thread = RunThread(self.count) self.count += 1 thread.signal.update_pb.connect(dialog.update_progressbar) # dialog.stop_thread.connect(thread.stop) # self.thread.start() self.pool.start(thread) # 线程池分配一个线程运行该任务 class Signal(QObject): update_pb = pyqtSignal(int) class RunThread(QRunnable): def __init__(self, count): super().__init__() self.count = count self.signal = Signal() # 信号 def run(self): for i in range(1, 101): print('thread_%s' % self.count, i, QThread().currentThreadId()) self.signal.update_pb.emit(i) time.sleep(1) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() sys.exit(app.exec_())
QThreadPool没有释放正在运行的线程的方法
以上这篇PYQT5开启多个线程和窗口,多线程与多窗口的交互实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年11月14日
2024年11月14日
- 刘德华《劲歌集》24K金碟港版[WAV+CUE][1.2G]
- Faker五冠王什么意思 世界赛五冠王Faker选手介绍
- faker塞拉斯s14决赛什么出装 faker塞拉斯s14决赛出装介绍
- LOLtoc9魔法口令在哪可以找到 2024云顶之弈toc观赛魔法口令兑换码
- 黑鸭子2008《影视经典·珍藏版》试音碟[WAV+CUE]
- 碧娜《温柔吻语2》[WAV+CUE]
- Stravinsky-Symphonies,Volume1-OrquestaSinfonicadeGalicia,DimaSlobodeniouk(2024)[24-
- 外媒评Switch2:向下兼容是关键 但挑战依然存在
- 任天堂提醒:宠物的尿或唾液或会让NS故障 把它放好!
- 《博德3》再创新高 Steam掌机总游玩时长近2000年
- 张玮伽《想你的夜DSD》东升 [WAV+CUE][1G]
- 姚璎格《 粤 24KGOLD》正版低速原抓[WAV+CUE][1G]
- 杨千嬅《如果大家都拥有海》寰亚 [WAV+CUE][998M]
- 孟庭苇.1994-1990-1994钻石精选集2CD(2022环球XRCD限量版)【上华】【WAV+CUE】
- 群星.1998-华纳好情歌精选17首【华纳】【WAV+CUE】