1、創建菜單欄
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("文件")
help=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("cut")
edit.addAction("paste")
quit=QAction("quit",self)
help.addAction("版本信息")
help.addAction("許可證信息")
file.addAction(quit)
def process(self,a):
print(self.sender().text())
if __name__ == "__main__":
app = QApplication(sys.argv)
p = menu()
p.show()
sys.exit(app.exec_())

2、創建工具欄,默認狀態為只顯示圖標,放上鼠標顯示名稱
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("./image-1/1-1.jpg"),"new",self) #設置工具欄的圖標new
tb1.addAction(new)
open = QAction(QIcon("./image-1/1-2.jpg"), "open", self) #設置工具欄的圖標open
tb1.addAction(open)
open.triggered.connect(self.print1) #工具欄的信號觸發函數triggered
save = QAction(QIcon("./image-1/1-3.png"), "save", self) # 設置工具欄的圖標save
tb1.addAction(save)
#添加第二個工具欄所包含的圖標肯文本說明
tb2=self.addToolBar("file1")
save2 = QAction(QIcon("./image-1/1-3.png"), "save", self) # 設置工具欄的圖標save
tb2.addAction(save2)
#設置工具欄的顯示格式
tb2.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) #圖標肯功能說明文本混合,文本在下面
tb2.setToolButtonStyle(Qt.ToolButtonTextOnly)
def print1(self):
print("hello world")
if __name__ == "__main__":
app = QApplication(sys.argv)
p = toolbar()
p.show()
sys.exit(app.exec_())

3、創建狀態欄,主要顯示目前的一些狀態信息,y一般位於窗口的下方
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.process)
self.setCentralWidget(QTextEdit())
self.statusbar=QStatusBar() #定義一個新的狀態欄
self.setStatusBar(self.statusbar) #設置窗口的狀態欄
def process(self,q):
if q.text()=="show":
self.statusbar.showMessage(q.text()+"菜單被點擊了",5000) #設置槽函數,狀態欄顯示信息
def print1(self):
print("hello world")
if __name__ == "__main__":
app = QApplication(sys.argv)
p =statusbar()
p.show()
sys.exit(app.exec_())
