PyQt5-快速上手笔记-02
状态栏
状态栏是用来显示应用状态信息的组件
from PyQt5.QtWidgets import QMainWindow
class myWidget(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.statusBar().showMessage("ready") #状态栏
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("状态栏")
self.show()
菜单栏
在MacOS下菜单栏的显示不一样,为了得到相似的结果我们可以加入:
menubar.setNativeMenuBar(False)
from PyQt5.QtWidgets import QMainWindow, QAction
from PyQt5.QtWidgets import qApp
from PyQt5.QtGui import QIcon
class myWidget(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
exitAct = QAction("&Exit", self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip("退出应用")
exitAct.triggered.connect(qApp.quit)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("简单菜单")
self.show()
from PyQt5.QtWidgets import QMainWindow, QAction
from PyQt5.QtWidgets import qApp
from PyQt5.QtGui import QIcon
class myWidget(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
exitAct = QAction("&Exit", self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip("退出应用")
exitAct.triggered.connect(qApp.quit)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("简单菜单")
self.show()
子菜单
子菜单是嵌套在菜单里的二级或者三级菜单
from PyQt5.QtWidgets import QMainWindow, QAction
from PyQt5.QtWidgets import QMenu
class myWidget(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
menubar = self.menuBar()
fileMenu =menubar.addMenu("File")
impMenu = QMenu("import", self)
imppAct = QAction("import mail", self)
impMenu.addAction(imppAct)
newAct = QAction("New", self)
fileMenu.addAction(newAct)
fileMenu.addMenu(impMenu)
self.setGeometry(300, 300, 200, 150)
self.setWindowTitle("子菜单")
self.show()
在菜单File下面创建了2个子菜单