PyQt5: Menus and toolbars
1. Main window
QMainWindow
Provides the functions of the main window, using it to create some simple status bars, toolbars and menu bars.
The main window is the collective name of the following windows, and the tutorial is below.
2. Status bar
The status bar is a component used to display application status information.
self.statusBar().showMessage('Ready')
The status bar isQMainWindow
created by.statusBar()
Method to create a status bar. The first call creates a status bar, and the second call returns a status bar object.showMessage()
Method displays a message on the status bar.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.statusBar().showMessage('Ready')
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Statusbar')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
3. Menu bar
The menu bar is very commonly used. It is a collection of commands (the display of the status bar under Mac OS is different. To get the most similar appearance, we can add a line of statements
menubar.setNativeMenuBar(False)
).
QAction
It is a combination of menu bar, toolbar or shortcut key actions. Among the three lines above, the first two lines create an icon, an exit label and a shortcut key combination, all of which perform an action; the third line creates a status bar, which can be displayed when the mouse is hovering over the menu bar. Show current status.
exitAct = QAction(QIcon('./Resources/images/icon.ico'), '&Exit', self)
icon, textexitAct.setShortcut('Ctrl+Q')
shortcut keyexitAct.setStatusTip('退出应用')
Status bar promptexitAct.triggered.connect(qApp.quit)
When this specified action is performed, an event is triggered. This eventQApplication的quit()
is associated with an action, so this action terminates the application.
self.menuBar()
Create a menu bar. Here a menu bar is created, a file menu is added to it using addMenu(), and the event of clicking to exit the application is associated with addAction().fileMenu = menubar.addMenu('&File')
Added a file menufileMenu.addAction(exitAct)
Associated with the event of clicking to exit the applicationexitAct
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QMenu
from PyQt5.QtGui import QIcon
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('菜单栏')
icon = QIcon(":/images/logo")
self.setWindowIcon(icon)
self.setGeometry(300, 300, 300, 200)
self.initUI()
self.show()
def initUI(self):
exitAct = QAction(QIcon('./Resources/images/icon.ico'), '&Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('退出应用')
exitAct.triggered.connect(qApp.quit)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
newMenu = menubar.addMenu('新建')
fileMenu.addAction(exitAct)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
3.1 Submenu
A submenu is a second- or third-level menu nested within a menu.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QMenu
from PyQt5.QtGui import QIcon
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('子单栏')
icon = QIcon("./Resources/images/icon.ico")
self.setWindowIcon(icon)
self.setGeometry(300, 300, 300, 200)
self.initUI()
self.show()
def initUI(self):
exitAct = QAction(QIcon('./Resources/images/icon.ico'), '&Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('退出应用')
exitAct.triggered.connect(qApp.quit)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
newMenu = QMenu('新建', self)
impMenu = QMenu('Import', self)
impAct = QAction('Import mail', self)
impMenu.addAction(impAct)
fileMenu.addMenu(newMenu)
fileMenu.addMenu(impMenu)
fileMenu.addAction(exitAct)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
3.2 Check menu
viewStatAct = QAction('View statusbar', self, checkable=True)
checkable
Create a selectable menu with options.viewStatAct.setStatusTip('View statusbar')
Status bar promptviewStatAct.setChecked(True)
Select menuviewStatAct.triggered.connect(self.toggleMenu)
When this specified action is performed, an event is triggeredself.toggleMenu
.- Show/hide status bar prompt
self.toggleMenu
during eventself.statusbar.show()、self.statusbar.hide()
import sys
from PyQt5.QtWidgets import QApplication, QAction, QMainWindow
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.statusbar = self.statusBar()
self.statusbar.showMessage('Ready')
menubar = self.menuBar()
viewMenu = menubar.addMenu('View')
viewStatAct = QAction('View statusbar', self, checkable=True)
viewStatAct.setStatusTip('View statusbar')
viewStatAct.setChecked(True)
viewStatAct.triggered.connect(self.toggleMenu)
viewMenu.addAction(viewStatAct)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Check menu')
self.show()
def toggleMenu(self, state):
if state:
self.statusbar.show()
else:
self.statusbar.hide()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
3.3 Right-click menu
The right-click menu is also called a pop-up box. It appears when the right mouse button is clicked in an application. It is a set of commands displayed on certain occasions. For example, in the Opera browser, the right-click menu on the web page will refresh, return or view the page source code. If you right-click on a toolbar, you get a different menu for managing the toolbar.
- Use
contextMenuEvent()
methods to implement this menuaction = cmenu.exec_(self.mapToGlobal(event.pos()))
Usageexec_()
Display menu. Get the current coordinates from the right mouse button event object.mapToGlobal()
The method converts the relative coordinates of the current component into the absolute coordinates of the window.if action == quitAct:
If an event is triggered in the right-click menuquitAct
, execute the behavior of closing the menuqApp.quit()
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow, qApp, QMenu, QApplication
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('右键菜单')
icon = QIcon('./Resources/images/icon.ico')
self.setWindowIcon(icon)
self.show()
def contextMenuEvent(self, event):
cmenu = QMenu(self)
newAct = cmenu.addAction("New")
opnAct = cmenu.addAction("Open")
quitAct = cmenu.addAction("Quit")
action = cmenu.exec_(self.mapToGlobal(event.pos()))
if action == quitAct:
qApp.quit()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
4. Toolbar
The menu bar contains all commands, and the toolbar is a collection of commonly used commands.
self.toolbar = self.addToolBar('Exit')
Create toolbarself.toolbar.addAction(exitAct)
Add action objects to the toolbar