每点击一次按钮,弹出一个对话框(子窗口),同时开启一个子线程来执行任务并更新对话框内容,关闭对话框则关闭对应子线程
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开启多个线程和窗口,多线程与多窗口的交互实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
PYQT5,多线程,多窗口,交互
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 明达年度发烧碟MasterSuperiorAudiophile2021[DSF]
- 英文DJ 《致命的温柔》24K德国HD金碟DTS 2CD[WAV+分轨][1.7G]
- 张学友1997《不老的传说》宝丽金首版 [WAV+CUE][971M]
- 张韶涵2024 《不负韶华》开盘母带[低速原抓WAV+CUE][1.1G]
- lol全球总决赛lcs三号种子是谁 S14全球总决赛lcs三号种子队伍介绍
- lol全球总决赛lck三号种子是谁 S14全球总决赛lck三号种子队伍
- 群星.2005-三里屯音乐之男孩女孩的情人节【太合麦田】【WAV+CUE】
- 崔健.2005-给你一点颜色【东西音乐】【WAV+CUE】
- 南台湾小姑娘.1998-心爱,等一下【大旗】【WAV+CUE】
- 【新世纪】群星-美丽人生(CestLaVie)(6CD)[WAV+CUE]
- ProteanQuartet-Tempusomniavincit(2024)[24-WAV]
- SirEdwardElgarconductsElgar[FLAC+CUE]
- 田震《20世纪中华歌坛名人百集珍藏版》[WAV+CUE][1G]
- BEYOND《大地》24K金蝶限量编号[低速原抓WAV+CUE][986M]
- 陈奕迅《准备中 SACD》[日本限量版] [WAV+CUE][1.2G]