* In programming development, a program inevitably requires multiple window operations to implement specific functions.
Basic steps to implement this function (taking three windows as an example, using the main window to call the other two windows)
# Main windowfrom PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): ("MainWindow") (800, 600) = (MainWindow) ("centralwidget") = () ((70, 180, 75, 23)) ("pushButton") self.pushButton_2 = () self.pushButton_2.setGeometry((250, 180, 75, 23)) self.pushButton_2.setObjectName("pushButton_2") () = (MainWindow) ((0, 0, 800, 23)) ("menubar") () = (MainWindow) ("statusbar") () (MainWindow) (MainWindow) def retranslateUi(self, MainWindow): _translate = (_translate("MainWindow", "MainWindow")) (_translate("MainWindow", "Open Window 1")) self.pushButton_2.setText(_translate("MainWindow", "Open Window 2")) # Window 1class Ui_Dialog(object): def setupUi(self, Dialog): ("Dialog") (400, 300) = (Dialog) ((30, 240, 341, 32)) () (|) ("buttonBox") = (Dialog) ((140, 100, 54, 12)) ("label") (Dialog) () () (Dialog) def retranslateUi(self, Dialog): _translate = (_translate("Dialog", "Dialog")) (_translate("Dialog", "This is window 1")) # Window 2 class Ui_Form(object): def setupUi(self, Form): ("Form") (400, 300) = (Form) ((140, 140, 54, 12)) ("label") (Form) (Form) def retranslateUi(self, Form): _translate = (_translate("Form", "Form")) (_translate("Form", "This is Window 2"))
Main program entry:
# Main Programclass MainWindow(QMainWindow, untitled.Ui_MainWindow): def __init__(self): super(MainWindow, self).__init__() (self) self.window2 = Ui_Dialog() self.() self.window3 = Ui_Form() self.() (self.)# Bind Window 2 self.pushButton_2.(self.) # Bind Window 3 if __name__ == '__main__': app = QApplication() MainWindow = MainWindow() () (app.exec_())
The above implementation of the main window pop-up window 1 and window 2 through buttons
The following implements the function of opening the file explorer through the window button to obtain file-related information:
1. Add a button in the main window:
class Ui_MainWindow(object): def setupUi(self, MainWindow): ...... self.pushButton_3 = () self.pushButton_3.setGeometry((420, 180, 75, 23)) self.pushButton_3.setObjectName("pushButton_3") def retranslateUi(self, MainWindow): ...... self.pushButton_3.setText(_translate("MainWindow", "Open Directory"))
2. Added to the main program:
# Main Programclass MainWindow(QMainWindow, untitled.Ui_MainWindow): def __init__(self): super(MainWindow, self).__init__() (self) self.window2 = Ui_Dialog() self.() self.window3 = Ui_Form() self.() (self.)# Bind Window 2 self.pushButton_2.(self.) # Bind Window 3 self.pushButton_3.() # Newly added # Newly added def gefilename(self): filename = () return filename if __name__ == '__main__': app = QApplication() MainWindow = MainWindow() () (app.exec_())
The above functions can be completed.
The above article on pyqt multi-windows is all the content I share with you. I hope you can give you a reference and I hope you support me more.