构造工具栏, 然后根据工具栏绑定事件,同时将文字放在图标的下方
""" 创建和使用工具栏 工具栏默认按钮: 只显示图标, 将文本作为悬停时 工具栏按钮有3种显示状态 1.只显示图标 2.只显示文本 3.只显示图标和文本 """ import sys, math from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * class Toolbar(QMainWindow): def __init__(self): super(Toolbar, self).__init__() self.initUI() def initUI(self): self.setWindowTitle("工具栏例子") self.resize(300, 200) tb1 = self.addToolBar("File") new = QAction(QIcon("D:\PyQt5_Study\Action Pack Win\Icons\Ares.ico"), "new", self) tb1.addAction(new) open = QAction(QIcon("D:\PyQt5_Study\Action Pack Win\Icons\Argo.ico"), "open", self) tb1.addAction(open) save = QAction(QIcon("D:\PyQt5_Study\Action Pack Win\Icons\Autolycus.ico"), "save", self) tb1.addAction(save) tb2 = self.addToolBar("File1") new1 = QAction(QIcon("D:\PyQt5_Study\Action Pack Win\Icons\Callisto.ico"), "new", self) tb2.addAction(new1) tb2.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) tb1.actionTriggered.connect(self.toolbtnpressed) tb2.actionTriggered.connect(self.toolbtnpressed) def toolbtnpressed(self, a): print("按下的工具栏按钮是", a.text()) if __name__ == "__main__": app = QApplication(sys.argv) main = Toolbar() main.show() sys.exit(app.exec_())