参考:https://www.jianshu.com/p/d8b3aff10ce7
import sys
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtWidgets import QDialog
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_())