qmenubar 添加按钮_PyQt5-菜单栏工具栏状态栏的使用(QMenuBar、QToolBar、QStatusBar)...
一、QMenuBar
窗体标题下方QMenuBar作为窗体菜单栏;QMenu对象提供了一个可以添加菜单栏的控件,也可以用于创建上下文菜单和弹出菜单选项;
每个QMenu对象都可以包含一个或者多个QAction对象或者级联的QMenu对象;
createPopupMenu()方法用于弹出一个菜单;
menuBar()方法用于返回主窗口的QMenuBar对象;
addMenu()方法可以将菜单添加到菜单栏;
addAction() 方法可以在菜单中进行添加某些操作;
常用方法:
例如:
1 #QMenuBar/QMenu/QAction的使用(菜单栏)
2 from PyQt5.QtWidgets importQMenuBar,QMenu,QAction,QLineEdit,QStyle,QFormLayout, QVBoxLayout,QWidget,QApplication ,QHBoxLayout, QPushButton,QMainWindow,QGridLayout,QLabel3 from PyQt5.QtCore importQDir4 from PyQt5.QtGui importQIcon,QPixmap,QFont5 from PyQt5.QtCore importQDate6
7 importsys8
9 classWindowClass(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 defprocesstrigger(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 importQToolBar, QMenuBar,QMenu,QAction,QLineEdit,QStyle,QFormLayout, QVBoxLayout,QWidget,QApplication ,QHBoxLayout, QPushButton,QMainWindow,QGridLayout,QLabel3 from PyQt5.QtGui importQIcon,QPixmap4
5
6 importsys7
8 classWindowClass(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 deftoolBtnPressed(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)
常用方法:
例如:修改上面实例,添加如下程序,这是状态栏信息;(点击工具栏按钮触发槽函数执行,完成对状态栏信息修改)