当使用多文档界面功能时,我们是将QMdiArea作为主窗口的*部件,然后在这个*部件中,我们可以同时打开很多个子窗口QMdiSubWindow
样式:
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QMdiArea, QMdiSubWindow, QAction, QTextEdit class Demo(QMainWindow):#继承QMainWindow
def __init__(self):
super(Demo, self).__init__()
self.mdi_area = QMdiArea(self) # 实例化一个多文档窗口
self.setCentralWidget(self.mdi_area) #把多文档窗口设置为主窗口的*控件 self.toolbar = self.addToolBar('Tool Bar') self.new_action = QAction('新建', self)
self.close_action = QAction('关闭', self)
self.close_all_action = QAction('全关闭', self)
self.mode1_action = QAction('层叠', self)
self.mode2_action = QAction('平铺', self) self.new_action.triggered.connect(self.new_func)
self.close_action.triggered.connect(self.mdi_area.closeActiveSubWindow)
#self.mdi_area.closeActiveSubWindow 关闭当前激活的窗口 self.close_all_action.triggered.connect(self.mdi_area.closeAllSubWindows)
#self.mdi_area.closeAllSubWindows 关闭所有子窗口 self.mode1_action.triggered.connect(self.mdi_area.cascadeSubWindows)
#self.mdi_area.cascadeSubWindows 层叠布局子窗口 self.mode2_action.triggered.connect(self.mdi_area.tileSubWindows)
#self.mdi_area.tileSubWindows 平铺布局子窗口 self.toolbar.addAction(self.new_action)
self.toolbar.addAction(self.close_action)
self.toolbar.addAction(self.close_all_action)
self.toolbar.addAction(self.mode1_action)
self.toolbar.addAction(self.mode2_action) def new_func(self):
text = QTextEdit()
sub = QMdiSubWindow() #实例化一个子窗口
sub.setWidget(text) #设置子窗口的控件
self.mdi_area.addSubWindow(sub) #在多文档窗口中添加子窗口
sub.show() #显示子窗口 if __name__ == '__main__':
app = QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec_())