PyQt5: Menus and toolbars

1. Main window

QMainWindowProvides 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 is QMainWindowcreated 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.

Insert image description here

#!/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)).

QActionIt 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, text
  • exitAct.setShortcut('Ctrl+Q')shortcut key
  • exitAct.setStatusTip('退出应用')Status bar prompt
  • exitAct.triggered.connect(qApp.quit)When this specified action is performed, an event is triggered. This event QApplication的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 menu
  • fileMenu.addAction(exitAct)Associated with the event of clicking to exit the applicationexitAct

Insert image description here

#!/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.

Insert image description here

#!/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)checkableCreate a selectable menu with options.
  • viewStatAct.setStatusTip('View statusbar')Status bar prompt
  • viewStatAct.setChecked(True)Select menu
  • viewStatAct.triggered.connect(self.toggleMenu)When this specified action is performed, an event is triggered self.toggleMenu.
  • Show/hide status bar prompt self.toggleMenuduring eventself.statusbar.show()、self.statusbar.hide()
    Insert image description here
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 menu
  • action = cmenu.exec_(self.mapToGlobal(event.pos()))Usage exec_()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 menu quitAct, execute the behavior of closing the menuqApp.quit()

Insert image description here

#!/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 toolbar
  • self.toolbar.addAction(exitAct)Add action objects to the toolbar

Insert image description here

Guess you like

Origin blog.csdn.net/qq_23452385/article/details/131878182