移除菜单栏
添加菜单栏
- ToolBar也类似。
代码中添加菜单
- 添加菜单选项用addMenu,添加真正要执行动作的项用addAction。
- 当要添加二级目录时,使用addMenu。
- 当要进行更详细的设定如快捷键等,创建QAction对象,设置好后,再将其利用addAction加入到菜单中。
class Menu(QMainWindow):
def __init__(self):
super(Menu, self).__init__()
self.init_ui()
def init_ui(self):
menu1 = self.menuBar()
file = menu1.addMenu("File")
load = file.addMenu("Load")
load.addAction("load_ok")
file.addAction("New")
file.addAction("Open")
save = QAction("Save", self)
save.setShortcut("CTRL + S")
file.addAction(save)
save.triggered.connect(self.process)
def process(self):
print(self.sender().text())
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = Menu()
ui.show()
sys.exit(app.exec())
- 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
QtDesigner中添加工具栏
- 工具栏中的东西和菜单栏是共用同一套东西的。
- 从ActionEditor里面选中直接拖动到工具栏上。
- 可以在Icon中指定显示图片。
代码中添加工具栏
- 首先使用addToolBar创建工具栏对象,可以有多个。
- 然后设置好QAction对象,再使用toolbar的addAction方法加入QAction对象。
- 工具栏的图标可以有对应的显示信息,或是以提示信息的形式存在,默认。或是存在于下方右方等,或是直接不显示。可以通过toolbar的setToolButtonStyle方法进行改变。
- 当需要不同的图标显示信息不同时,可以创建多个工具栏对象。
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Toolbar(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
tool_bar1 = self.addToolBar("File")
new_file = QAction(QIcon("dog.png"), "new file", self)
tool_bar1.addAction(new_file)
new_file.triggered.connect(self.process)
open_file = QAction(QIcon("tiger.png"), "open file", self)
tool_bar1.addAction(open_file)
open_file.triggered.connect(self.process)
tool_bar1.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
tool_bar1.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
tool_bar2 = self.addToolBar("File")
new_file = QAction(QIcon("dog.png"), "new file", self)
tool_bar2.addAction(new_file)
new_file.triggered.connect(self.process)
tool_bar2.setToolButtonStyle(Qt.ToolButtonIconOnly)
def process(self):
print(self.sender().text())
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = Toolbar()
ui.show()
sys.exit(app.exec())
- 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
QtDesigner中添加状态栏
代码中添加状态栏
import sys
from PyQt5.QtWidgets import *
class Statusbar(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.resize(500, 500)
menu_bar = self.menuBar()
file = menu_bar.addMenu("File")
new_file = file.addAction("new")
new_file.triggered.connect(self.process)
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
def process(self):
print("aaaa")
self.status_bar.showMessage("you want to create a new file!", 3000)
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = Statusbar()
ui.show()
sys.exit(app.exec())
- 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