pyQT 菜单栏 工具栏 状态栏使用
PyQt5-菜单栏工具栏状态栏的使用(QMenuBar、QToolBar、QStatusBar)
转自:PyQt5-菜单栏工具栏状态栏的使用(QMenuBar、QToolBar、QStatusBar) - ygzhaof_100 - 博客园
https://www.cnblogs.com/ygzhaof/p/10070523.html
一、QMenuBar
窗体标题下方QMenuBar作为窗体菜单栏;QMenu对象提供了一个可以添加菜单栏的控件,也可以用于创建上下文菜单和弹出菜单选项;
每个QMenu对象都可以包含一个或者多个QAction对象或者级联的QMenu对象;
createPopupMenu()方法用于弹出一个菜单;
menuBar()方法用于返回主窗口的QMenuBar对象;
addMenu()方法可以将菜单添加到菜单栏;
addAction() 方法可以在菜单中进行添加某些操作;
常用方法:

例如:
-
#QMenuBar/QMenu/QAction的使用(菜单栏)
-
from PyQt5.QtWidgets import QMenuBar,QMenu,QAction,QLineEdit,QStyle,QFormLayout, QVBoxLayout,QWidget,QApplication ,QHBoxLayout, QPushButton,QMainWindow,QGridLayout,QLabel
-
from PyQt5.QtCore import QDir
-
from PyQt5.QtGui import QIcon,QPixmap,QFont
-
from PyQt5.QtCore import QDate
-
-
import sys
-
-
class WindowClass(QMainWindow):
-
-
def __init__(self,parent=None):
-
-
super(WindowClass, self).__init__(parent)
-
self.layout=QHBoxLayout()
-
self.menubar=self.menuBar()#获取窗体的菜单栏
-
-
self.file=self.menubar.addMenu("系统菜单")
-
self.file.addAction("New File")
-
-
self.save=QAction("Save",self)
-
self.save.setShortcut("Ctrl+S")#设置快捷键
-
self.file.addAction(self.save)
-
-
self.edit=self.file.addMenu("Edit")
-
self.edit.addAction("copy")#Edit下这是copy子项
-
self.edit.addAction("paste")#Edit下设置paste子项
-
-
self.quit=QAction("Quit",self)#注意如果改为:self.file.addMenu("Quit") 则表示该菜单下必须柚子菜单项;会有>箭头
-
self.file.addAction(self.quit)
-
self.file.triggered[QAction].connect(self.processtrigger)
-
self.setLayout(self.layout)
-
self.setWindowTitle("Menu Demo")
-
-
def processtrigger(self,qaction):
-
print(qaction.text()+" is triggered!")
-
-
if __name__=="__main__":
-
app=QApplication(sys.argv)
-
win=WindowClass()
-
win.show()
-
sys.exit(app.exec_())

二、QToolBar工具栏
该控件是由文本按钮、图标或者其他小控件按钮组成的可移动面板,通常位于菜单栏下方,作为工具栏使用;
每次单击工具栏中的按钮,此时都会触发actionTriggered信号。这个信号将关联QAction对象的引用发送到链接的槽函数上;
常用方法如下:

例如:结合上面menubar:
-
#QToolBar(工具栏)
-
from PyQt5.QtWidgets import QToolBar, QMenuBar,QMenu,QAction,QLineEdit,QStyle,QFormLayout, QVBoxLayout,QWidget,QApplication ,QHBoxLayout, QPushButton,QMainWindow,QGridLayout,QLabel
-
from PyQt5.QtGui import QIcon,QPixmap
-
-
-
import sys
-
-
class WindowClass(QMainWindow):
-
-
def __init__(self,parent=None):
-
super(WindowClass, self).__init__(parent)
-
self.layout=QVBoxLayout()
-
self.resize(200,300)
-
#菜单栏
-
menuBar=self.menuBar()
-
menu=menuBar.addMenu("File")
-
action=QAction(QIcon("./image/save.ico"),"New Project", self)
-
menu.addAction(action)
-
-
menu2=menu.addMenu("Add to ...")
-
menu2.addAction(QAction("workspace edit...",self))
-
-
-
#工具栏
-
tool=self.addToolBar("File")
-
#edit=QAction(QIcon("./image/save.ico"),"刷新",self)
-
edit=QAction(QIcon("./image/save.ico"),"save",self)
-
tool.addAction(edit)
-
#tool.setOrientation(Qt.Horizontal) # 默认横向
-
#tool.setOrientation(Qt.Vertical) # 纵向摆放
-
#tool.setFixedWidth(64) #设定宽度
-
#tool.setFixedHeight(64) #设定高度
-
-
wifi=QAction(QIcon(QPixmap("./image/wifi.png")),"wifi",self)
-
tool.addAction(wifi)
-
tool.actionTriggered[QAction].connect(self.toolBtnPressed)
-
-
def toolBtnPressed(self,qaction):
-
print("pressed too btn is",qaction.text())
-
if __name__=="__main__":
-
app=QApplication(sys.argv)
-
win=WindowClass()
-
win.show()
-
sys.exit(app.exec_())


三、状态栏:QStatusBar
通过主窗口QMainWindow的setStatusBar()函数设置状态栏信息;
例如:
self.statusBar=QstatusBar()
self.setStatusBar(self.statusBar)
常用方法:


例如:修改上面实例,添加如下程序,这是状态栏信息;(点击工具栏按钮触发槽函数执行,完成对状态栏信息修改)


目录

浙公网安备 33010602011771号