Interactive invocation method between pyqt multiple windows

  • 2021-06-28 13:36:01
  • OfStack

*In programming development, a program inevitably needs multi-window operation to achieve specific functions.

The basic steps to achieve this function (using the main window to call the other two windows, for example, with three windows)


#  main window 
from PyQt5 import QtCore, QtGui, QtWidgets
 
class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(800, 600)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
 
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(70, 180, 75, 23))
    self.pushButton.setObjectName("pushButton")
 
    self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_2.setGeometry(QtCore.QRect(250, 180, 75, 23))
    self.pushButton_2.setObjectName("pushButton_2")
 
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 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)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)
 
  def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.pushButton.setText(_translate("MainWindow", " open windows 1"))
    self.pushButton_2.setText(_translate("MainWindow", " open windows 2 "))
 
#  window 1
class Ui_Dialog(object):
  def setupUi(self, Dialog):
    Dialog.setObjectName("Dialog")
    Dialog.resize(400, 300)
    self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
    self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
    self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
    self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
    self.buttonBox.setObjectName("buttonBox")
    self.label = QtWidgets.QLabel(Dialog)
    self.label.setGeometry(QtCore.QRect(140, 100, 54, 12))
    self.label.setObjectName("label")
 
    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"))
    self.label.setText(_translate("Dialog", " This is the window 1"))
#  window 2
 
class Ui_Form(object):
  def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(400, 300)
    self.label = QtWidgets.QLabel(Form)
    self.label.setGeometry(QtCore.QRect(140, 140, 54, 12))
    self.label.setObjectName("label")
 
    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)
 
  def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "Form"))
    self.label.setText(_translate("Form", " This is the window 2"))
 

Main program entry:


#  main program 
class MainWindow(QMainWindow, untitled.Ui_MainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()
    self.setupUi(self)
    self.window2 = Ui_Dialog()
    self.window2.setupUi()
    self.window3 = Ui_Form()
    self.window3.setupUi()
    self.pushButton.clicked.connect(self.window2.show)#  Binding window 2 
    self.pushButton_2.clicked.connect(self.window3.show) #  Binding window 3
 
 
if __name__ == '__main__':
  app = QApplication(sys.argv)
  MainWindow = MainWindow()
  MainWindow.show()
  sys.exit(app.exec_())

Above implements main window through button pop-up windows 1 and 2

The following implements the function of opening the file explorer through the window button to get information about the file:

1. Add a button in the main window:


class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    ......
    self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_3.setGeometry(QtCore.QRect(420, 180, 75, 23))
    self.pushButton_3.setObjectName("pushButton_3")
  def retranslateUi(self, MainWindow):
    ......
    self.pushButton_3.setText(_translate("MainWindow", " Open Catalog "))

2. Add in main program:


#  main program 
class MainWindow(QMainWindow, untitled.Ui_MainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()
    self.setupUi(self)
    self.window2 = Ui_Dialog()
    self.Window2.setupUi()
    self.window3 = Ui_Form()
    self.Window3.setupUi()
    self.pushButton.clicked.connect(self.window2.show)#  Binding window 2 
    self.pushButton_2.clicked.connect(self.window3.show) #  Binding window 3
 
    self.pushButton_3.clicked.connect(self.gefilename) #  New Added 
  #  New Added 
  def gefilename(self):
    filename = QFileDialog.getOpenFileName()
    return filename
 
 
if __name__ == '__main__':
  app = QApplication(sys.argv)
  MainWindow = MainWindow()
  MainWindow.show()
  sys.exit(app.exec_())

You can accomplish the above functions.


Related articles: