PyQt5-菜單欄工具欄狀態欄的使用(QMenuBar、QToolBar、QStatusBar)


一、QMenuBar

窗體標題下方QMenuBar作為窗體菜單欄;QMenu對象提供了一個可以添加菜單欄的控件,也可以用於創建上下文菜單和彈出菜單選項;

每個QMenu對象都可以包含一個或者多個QAction對象或者級聯的QMenu對象;

createPopupMenu()方法用於彈出一個菜單;

menuBar()方法用於返回主窗口的QMenuBar對象;

addMenu()方法可以將菜單添加到菜單欄;

addAction() 方法可以在菜單中進行添加某些操作;

常用方法:

例如:

 1 #QMenuBar/QMenu/QAction的使用(菜單欄)
 2 from PyQt5.QtWidgets import   QMenuBar,QMenu,QAction,QLineEdit,QStyle,QFormLayout,   QVBoxLayout,QWidget,QApplication ,QHBoxLayout, QPushButton,QMainWindow,QGridLayout,QLabel
 3 from PyQt5.QtCore import QDir
 4 from PyQt5.QtGui import QIcon,QPixmap,QFont
 5 from PyQt5.QtCore import  QDate
 6 
 7 import sys
 8 
 9 class WindowClass(QMainWindow):
10 
11     def __init__(self,parent=None):
12 
13         super(WindowClass, self).__init__(parent)
14         self.layout=QHBoxLayout()
15         self.menubar=self.menuBar()#獲取窗體的菜單欄
16 
17         self.file=self.menubar.addMenu("系統菜單")
18         self.file.addAction("New File")
19 
20         self.save=QAction("Save",self)
21         self.save.setShortcut("Ctrl+S")#設置快捷鍵
22         self.file.addAction(self.save)
23 
24         self.edit=self.file.addMenu("Edit")
25         self.edit.addAction("copy")#Edit下這是copy子項
26         self.edit.addAction("paste")#Edit下設置paste子項
27 
28         self.quit=QAction("Quit",self)#注意如果改為:self.file.addMenu("Quit") 則表示該菜單下必須柚子菜單項;會有>箭頭
29         self.file.addAction(self.quit)
30         self.file.triggered[QAction].connect(self.processtrigger)
31         self.setLayout(self.layout)
32         self.setWindowTitle("Menu Demo")
33 
34     def processtrigger(self,qaction):
35         print(qaction.text()+" is triggered!")
36 
37 if __name__=="__main__":
38     app=QApplication(sys.argv)
39     win=WindowClass()
40     win.show()
41     sys.exit(app.exec_())

 

 

 

二、QToolBar工具欄

該控件是由文本按鈕、圖標或者其他小控件按鈕組成的可移動面板,通常位於菜單欄下方,作為工具欄使用;

每次單擊工具欄中的按鈕,此時都會觸發actionTriggered信號。這個信號將關聯QAction對象的引用發送到鏈接的槽函數上;

常用方法如下:

例如:結合上面menubar:

 1 #QToolBar(工具欄)
 2 from PyQt5.QtWidgets import QToolBar,   QMenuBar,QMenu,QAction,QLineEdit,QStyle,QFormLayout,   QVBoxLayout,QWidget,QApplication ,QHBoxLayout, QPushButton,QMainWindow,QGridLayout,QLabel
 3 from PyQt5.QtGui import QIcon,QPixmap
 4 
 5 
 6 import sys
 7 
 8 class WindowClass(QMainWindow):
 9 
10     def __init__(self,parent=None):
11         super(WindowClass, self).__init__(parent)
12         self.layout=QVBoxLayout()
13         self.resize(200,300)
14         #菜單欄
15         menuBar=self.menuBar()
16         menu=menuBar.addMenu("File")
17         action=QAction(QIcon("./image/save.ico"),"New Project", self)
18         menu.addAction(action)
19 
20         menu2=menu.addMenu("Add to ...")
21         menu2.addAction(QAction("workspace edit...",self))
22 
23 
24         #工具欄
25         tool=self.addToolBar("File")
26         #edit=QAction(QIcon("./image/save.ico"),"刷新",self)
27         edit=QAction(QIcon("./image/save.ico"),"save",self)
28         tool.addAction(edit)
29 
30         wifi=QAction(QIcon(QPixmap("./image/wifi.png")),"wifi",self)
31         tool.addAction(wifi)
32         tool.actionTriggered[QAction].connect(self.toolBtnPressed)
33 
34     def toolBtnPressed(self,qaction):
35         print("pressed too btn is",qaction.text())
36 if __name__=="__main__":
37     app=QApplication(sys.argv)
38     win=WindowClass()
39     win.show()
40     sys.exit(app.exec_())

 

 

 

三、狀態欄:QStatusBar

通過主窗口QMainWindow的setStatusBar()函數設置狀態欄信息;

例如:

self.statusBar=QstatusBar()

self.setStatusBar(self.statusBar)

常用方法:

例如:修改上面實例,添加如下程序,這是狀態欄信息;(點擊工具欄按鈕觸發槽函數執行,完成對狀態欄信息修改)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM