当前位置 主页 > 网站技术 > 代码类 >

    PYQT5开启多个线程和窗口,多线程与多窗口的交互实例

    栏目:代码类 时间:2019-12-13 21:07

    每点击一次按钮,弹出一个对话框(子窗口),同时开启一个子线程来执行任务并更新对话框内容,关闭对话框则关闭对应子线程

    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_())