添加菜单栏的组件, 通过点击菜单栏的组件按钮来生成新的窗口, 然后对新的窗口进行重叠或者扩张的操作
MultiWindows.py
""" 容纳多文档的窗口 QMdiArea QMdiSubWindow """ import sys, math from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * class MultiWindows(QMainWindow): count = 0 def __init__(self): super(MultiWindows, self).__init__() self.setWindowTitle("容纳多文档的窗口") #容纳多文档窗口 self.mdi = QMdiArea() self.setCentralWidget(self.mdi) bar = self.menuBar() file = bar.addMenu("File") file.addAction("New") file.addAction("cascade") file.addAction("Tiled") file.triggered.connect(self.windowaction) def windowaction(self, q): print(q) if q.text() == "New": MultiWindows.count += 1 #设置单个的子窗口 sub = QMdiSubWindow() #在子窗口上添加一个文本编辑框 sub.setWidget(QTextEdit()) sub.setWindowTitle("子窗口" + str(MultiWindows.count)) #将子窗口添加到多个窗口控件上 self.mdi.addSubWindow(sub) sub.show() elif q.text() == "cascade": #设置窗口的缩放方式为重叠 self.mdi.cascadeSubWindows() elif q.text() == 'Tiled': #设置窗口的缩放方式为展开 self.mdi.tileSubWindows() if __name__ == "__main__": app = QApplication(sys.argv) main = MultiWindows() main.show() sys.exit(app.exec_())