forked from muziing/PyQt_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-QMainWindow-QToolBar-工具栏.py
49 lines (39 loc) · 1.35 KB
/
03-QMainWindow-QToolBar-工具栏.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
import sys
from PyQt5.Qt import *
"""
Qt.ToolBarArea:
NoToolBarArea = 0
LeftToolBarArea = 1
RightToolBarArea = 2
TopToolBarArea = 4
BottomToolBarArea = 8
"""
class Window(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("QToolBar-工具栏")
self.resize(500, 500)
self.move(400, 250)
self.toolbar = QToolBar(self)
self.text_edit = QTextEdit(self)
self.setup_ui()
def set_tool_bar(self):
self.toolbar.setFloatable(False) # 是否允许工具栏脱离主窗口悬浮,默认为True
self.toolbar.addAction("关闭窗口", lambda: self.close())
# 增加action时可设置图标、名称、槽函数
self.toolbar.addAction(
QIcon("../Icons/text-icons/undo_96px.ico"), "撤销", self.text_edit.undo
)
self.toolbar.addAction(
QIcon("../Icons/text-icons/redo_96px.ico"), "重做", self.text_edit.redo
)
def setup_ui(self):
self.setCentralWidget(self.text_edit)
# ------工具栏--------
self.set_tool_bar() # 设置工具栏
self.addToolBar(Qt.TopToolBarArea, self.toolbar) # 指定创建时工具栏停靠的区域
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())