菜单栏
‘‘‘ 创建和使用菜单 ‘‘‘ import sys,math from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * class Menu(QMainWindow) : def __init__(self): super(Menu,self).__init__() bar = self.menuBar() # 获取菜单栏 file = bar.addMenu("文件") file.addAction("新建") save = QAction("保存",self) save.setShortcut("Ctrl + S") file.addAction(save) save.triggered.connect(self.process) edit = bar.addMenu("Edit") edit.addAction("copy") edit.addAction("paste") quit = QAction("退出",self) file.addAction(quit) def process(self,a): print(self.sender().text()) if __name__ == ‘__main__‘: app = QApplication(sys.argv) main = Menu() main.show() sys.exit(app.exec_())
工具栏
‘‘‘ 创建和使用工具栏 工具栏默认按钮:只显示图标,将文本作为悬停提示展示 工具栏按钮有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(‘./images/new.png‘),"new",self) tb1.addAction(new) open = QAction(QIcon(‘./images/open.png‘),"open",self) tb1.addAction(open) save = QAction(QIcon(‘./images/save.png‘),"save",self) tb1.addAction(save) tb2 = self.addToolBar("File1") new1 = QAction(QIcon(‘./images/new.png‘),"新建",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_())
状态栏
‘‘‘ 创建和使用状态栏 ‘‘‘ import sys,math from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * class StatusBar(QMainWindow) : def __init__(self): super(StatusBar,self).__init__() self.initUI() def initUI(self): self.setWindowTitle("状态栏演示") self.resize(300,200) bar = self.menuBar() file = bar.addMenu("File") file.addAction("show") file.triggered.connect(self.processTrigger) self.setCentralWidget(QTextEdit()) self.statusBar = QStatusBar() self.setStatusBar(self.statusBar) def processTrigger(self,q): if q.text() == "show" : self.statusBar.showMessage(q.text() + " 菜单被点击了",5000) if __name__ == ‘__main__‘: app = QApplication(sys.argv) main = StatusBar() main.show() sys.exit(app.exec_())
原文:https://www.cnblogs.com/wuyuan2011woaini/p/14822754.html