每點擊一次按鈕,彈出一個對話框(子窗口),同時開啟一個子線程來執(zhí)行任務(wù)并更新對話框內(nèi)容,關(guān)閉對話框則關(guān)閉對應(yīng)子線程
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"))
- 每點擊一次按鈕,打開一個彈窗
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. 打開彈窗的同時,打開一個子線程,更新對話框中的進(jìn)度條
在子線程定義信號,關(guān)聯(lián)對話框更新進(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)度條的信號
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_())
- 關(guān)閉對話框,則關(guān)閉對應(yīng)子線程
在對話框中添加自定義信號,并重寫關(guān)閉事件,在關(guān)閉窗口時發(fā)送關(guān)閉子線程的信號
class DialogWindow(QDialog, Ui_Dialog):
stop_thread = pyqtSignal() # 定義關(guān)閉子線程的信號
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_())
- 使用線程池QThreadPool管理子線程
使用QThreadPool, 線程需要繼承QRunnable,而QRunnable只是namespace,沒有繼承QT的信號機(jī)制,
所以需要另外繼承QObject來使用信號,我這里直接在線程中使用封裝的信號向外部傳遞信息
class DialogWindow(QDialog, Ui_Dialog):
stop_thread = pyqtSignal() # 定義關(guān)閉子線程的信號
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è)置最大線程數(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) # 線程池分配一個線程運行該任務(wù)
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開啟多個線程和窗口,多線程與多窗口的交互實例就是小編分享給大家的全部內(nèi)容了掰烟,希望能給大家一個參考,也希望大家多多支持腳本之家沐批。