SoFunction
Updated on 2024-11-17

python GUI library graphical interface development PyQt5 menu bar control QMenuBar detailed usage and examples

Introduction to the PyQt5 MenuBar control QMenuBar

QMenuBar is below the title bar of the QMainWindow object, and the horizontal QMenuBar is reserved for displaying the QMenu object.

QMenuBar class provides a QMenu object that can contain one or more QAction objects or cascade, to create a popup menu, Pyqt provides the createPopupMenu() function, the menuBar() function is used to return to the main window's QMenuBar object: the addMenu() function can add the menu to the menu bar, through the addAction () function can be added to the menu operation

Some important methods commonly used when designing menu systems

methodologies descriptive
menuBar() Returns the QMenuBar object of the main window.
addMenu() Add a new QMenu object to the menu bar
addAction() Add an action button to the QMenu mini-control that contains text or an icon.
setEnabled() Setting the action buttons to enable/disable
addSeperator() Add a split line to the menu
clear() Delete the contents of the menu bar
setShortcut() Associating shortcuts to action buttons
setText() Setting the text of menu items
setTitle() Set the title of the QMenu widget
text() Returns the text associated with the QACtion object
title() Returns the title of the QMenu widget

When any QAction button is clicked, the QMenu object emits the triggered signal

Example of using QMenuBar

import sys
from  import *
from  import *
from  import *

class MenuDemo(QMainWindow):
  def __init__(self,parent=None):
    super(MenuDemo, self).__init__(parent)

    # Horizontal Layout
    layout=QHBoxLayout()

    # Instantiate the main window's QMenuBar object
    bar=()
    # Add a new QMenu object to the menu bar, parent menu
    file=('File')
    # Add buttons, submenus to QMenu widgets
    ('New')

    # Define responsive widget buttons and set shortcuts associated with action buttons to be added under parent menu
    save=QAction('Save',self)
    ('Ctrl+S')
    (save)

    #Create new sub-menu item and add Sun menu
    edit=('Edit')
    ('Copy')
    ('Paste')

    #Add under the parent menu
    quit=QAction('Quit',self)
    (quit)

    #Clicking on any Qmenu object emits a signal that binds the slot function
    [QAction].connect()

    #Setup Layout & Title
    (layout)
    ('menu example')

  def processtrigger(self,q):
    # Output that the Qmenu object was clicked
    print(()+'is triggeres')

if __name__ == '__main__':
  app=QApplication()
  demo=MenuDemo()
  ()
  (app.exec_())

Run the program, the display effect is shown in Figure

QMenuBar code analysis

In this example, the top-level window must be a QMainWindow object to reference the QMenuBar object

Add the 'File' menu to the menu bar via the addMenu() method

    bar=()

    # Add a new QMenu object to the menu bar, parent menu

    file=('File')

Action buttons in the menu bar can be strings or QAction objects

    ('New')
    # Define responsive widget buttons and set shortcuts associated with action buttons to be added under parent menu

    save=QAction('Save',self)
    ('Ctrl+S')
    (save)

Adding submenus to the top level menu

    edit=('Edit')
    ('Copy')
    ('Paste')

The menu emits the triggered signal, connecting the signal to the slot function processtrigger () The QAction object that accepts the signal

[QAction].connect()

This article explains in detail the use of PyQt5 menu bar control QMenuBar and examples, more about the use of PyQt5 menu bar control knowledge, please see the following related links