PYQT5開(kāi)啟多個(gè)線(xiàn)程和窗口,多線(xiàn)程與多窗口的交互實(shí)例
每點(diǎn)擊一次按鈕,彈出一個(gè)對(duì)話(huà)框(子窗口),同時(shí)開(kāi)啟一個(gè)子線(xiàn)程來(lái)執(zhí)行任務(wù)并更新對(duì)話(huà)框內(nèi)容,關(guān)閉對(duì)話(huà)框則關(guān)閉對(duì)應(yīng)子線(xiàn)程
1. 建立一個(gè)簡(jiǎn)單的主界面和一個(gè)自定義對(duì)話(huà)框
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", "多線(xiàn)程彈窗")) 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. 每點(diǎn)擊一次按鈕,打開(kāi)一個(gè)彈窗
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. 打開(kāi)彈窗的同時(shí),打開(kāi)一個(gè)子線(xiàn)程,更新對(duì)話(huà)框中的進(jìn)度條
在子線(xiàn)程定義信號(hào),關(guān)聯(lián)對(duì)話(huà)框更新進(jìn)度條的槽函數(shù)
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) # 更新進(jìn)度條 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) # 關(guān)聯(lián) self.thread.start() class RunThread(QThread): update_pb = pyqtSignal(int) # 定義更新進(jìn)度條的信號(hào) 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. 關(guān)閉對(duì)話(huà)框,則關(guān)閉對(duì)應(yīng)子線(xiàn)程
在對(duì)話(huà)框中添加自定義信號(hào),并重寫(xiě)關(guān)閉事件,在關(guān)閉窗口時(shí)發(fā)送關(guān)閉子線(xiàn)程的信號(hào)
class DialogWindow(QDialog, Ui_Dialog): stop_thread = pyqtSignal() # 定義關(guān)閉子線(xiàn)程的信號(hào) 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. 使用線(xiàn)程池QThreadPool管理子線(xiàn)程
使用QThreadPool, 線(xiàn)程需要繼承QRunnable,而QRunnable只是namespace,沒(méi)有繼承QT的信號(hào)機(jī)制,
所以需要另外繼承QObject來(lái)使用信號(hào),我這里直接在線(xiàn)程中使用封裝的信號(hào)向外部傳遞信息
class DialogWindow(QDialog, Ui_Dialog): stop_thread = pyqtSignal() # 定義關(guān)閉子線(xiàn)程的信號(hào) 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) # 設(shè)置最大線(xiàn)程數(shù) 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) # 線(xiàn)程池分配一個(gè)線(xiàn)程運(yùn)行該任務(wù) class Signal(QObject): update_pb = pyqtSignal(int) class RunThread(QRunnable): def __init__(self, count): super().__init__() self.count = count self.signal = Signal() # 信號(hào) 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沒(méi)有釋放正在運(yùn)行的線(xiàn)程的方法
以上這篇PYQT5開(kāi)啟多個(gè)線(xiàn)程和窗口,多線(xiàn)程與多窗口的交互實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 詳解PyQt5中Thread多線(xiàn)程的使用
- Python Pyqt5多線(xiàn)程更新UI代碼實(shí)例(防止界面卡死)
- 詳解PyQt5 GUI 接收UDP數(shù)據(jù)并動(dòng)態(tài)繪圖的過(guò)程(多線(xiàn)程間信號(hào)傳遞)
- Pyqt5 實(shí)現(xiàn)多線(xiàn)程文件搜索的案例
- PyQt5多線(xiàn)程防卡死和多窗口用法的實(shí)現(xiàn)
- python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5多線(xiàn)程中信號(hào)與槽的詳細(xì)使用方法與實(shí)例
- 利用PyQt中的QThread類(lèi)實(shí)現(xiàn)多線(xiàn)程
- PyQt5中多線(xiàn)程模塊QThread使用方法的實(shí)現(xiàn)
- PyQt 異步任務(wù)多線(xiàn)程的幾種方案示例詳解
相關(guān)文章
Python模塊對(duì)Redis數(shù)據(jù)庫(kù)的連接與使用講解
這篇文章主要介紹了Python模塊對(duì)Redis數(shù)據(jù)庫(kù)的連接與使用,通過(guò)實(shí)例代碼給大家介紹了Python連接Redis數(shù)據(jù)庫(kù)方法,Python使用連接池連接Redis數(shù)據(jù)庫(kù)方法,感興趣的朋友跟隨小編一起看看吧2021-07-07Python selenium爬取微信公眾號(hào)文章代碼詳解
這篇文章主要介紹了Python selenium爬取微信公眾號(hào)歷史文章代碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Facebook開(kāi)源一站式服務(wù)python時(shí)序利器Kats詳解
這篇文章主要為答案及介紹了Facebook開(kāi)源一站式服務(wù)python時(shí)序利器Kats的功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11python+mongodb數(shù)據(jù)抓取詳細(xì)介紹
這篇文章主要介紹了python+mongodb數(shù)據(jù)抓取詳細(xì)介紹,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法示例
這篇文章主要介紹了python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Python單向循環(huán)鏈表概念、原理、定義及使用方法,需要的朋友可以參考下2019-12-12python用match()函數(shù)爬數(shù)據(jù)方法詳解
在本篇文章里小編給大家整理了關(guān)于python用match()函數(shù)爬數(shù)據(jù)方法以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。2019-07-07解決pytorch讀取自制數(shù)據(jù)集出現(xiàn)過(guò)的問(wèn)題
這篇文章主要介紹了解決pytorch讀取自制數(shù)據(jù)集出現(xiàn)過(guò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05