forked from muziing/PyQt_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-QMdiArea-容纳多文档(子窗口)的窗口.py
50 lines (41 loc) · 1.39 KB
/
04-QMdiArea-容纳多文档(子窗口)的窗口.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
from PyQt5.QtWidgets import *
"""
容纳多文档的窗口
容纳多文档的 QMdiArea
多文档子窗口类 QMdiSubWindow
将QMdiSubWindow添加至QMdiArea中
原帖: https://blog.csdn.net/qq_40243295/article/details/105633221
"""
class MultiDemo(QMainWindow):
count = 0
def __init__(self, parent=None):
super(MultiDemo, self).__init__(parent)
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.window_action)
def window_action(self, q):
print(q.text())
if q.text() == "New":
MultiDemo.count = MultiDemo.count + 1
sub = QMdiSubWindow()
sub.setWidget(QTextEdit())
sub.setWindowTitle("子窗口" + str(MultiDemo.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 = MultiDemo()
main.show()
sys.exit(app.exec_())