Using the standard coding structure introduced in the previous tutorial, we will focus createMenu() in this post.

Creating Menubar and Menu Items

To create the menu bar with a list of menus underneath, we need to create a set of menu headers and menu items. Then we assign each of the menu items to the desired menu header.

def createMenu(self):
    # add menu header
    self.menuBar = QtWidgets.QMenuBar()
    self.fileMenu = self.menuBar.addMenu("File")
    self.aboutMenu = self.menuBar.addMenu("Help")
    
    # create menu items
    self.actionOpenFile = QtWidgets.QAction("Open File", self)
    self.actionSaveFile = QtWidgets.QAction("Save", self)
    self.actionQuit = QtWidgets.QAction("Quit", self)
    self.actionAbout = QtWidgets.QAction("Info", self)
       
    # bind menu item to menu header
    self.fileMenu.addAction(self.actionOpenFile)
    self.fileMenu.addAction(self.actionSaveFile)
    self.fileMenu.addAction(self.actionQuit)
    self.aboutMenu.addAction(self.actionAbout)

def createLayout(self):
    mainLayout = QtWidgets.QVBoxLayout()
    mainLayout.setMenuBar(self.menuBar)
    mainLayout.addWidget(self.labelName)
    mainLayout.addWidget(self.editText)
    mainLayout.addWidget(self.buttonTextUpdate)
    self.setLayout(mainLayout)

The macOS platform – The Menu behaves differently as the menu appears on the top bar of the screen. Also, if you selected labels that are reserved by the macOS system, your menus will not be shown. Reserved labels are ‘Quit’, ‘Exit’, ‘Setting’, ‘Settings’, ‘preferences’ and many more. Lastly, if your menu bar header does not contain any children, your menu bar will not appear at all. Line 5-10 demonstrate this shortcoming in a macOS environment.

# Not shown in MacOS Menubar due to reserved label 
self.actionQuit = QtWidgets.QAction("Quit", self) 

# Not Shown in MacOS Menubar due to reserved label
self.actionQuit2 = QtWidgets.QAction("Quit...", self) 

# Shown in MacOS Menubar
self.actionQuit3 = QtWidgets.QAction("3. Quit", self)
The visual output of the menu launched on the macOS platform.
The visual output of the menu in Windows 10.

Since we use Help as the menu label, a search function automatically appeared in the macOS platform.

Added the help menu item.
The Help menu on the Windows 10 platform.

Cascaded Menu Items

We can further cascade menu items under a menu item. For example, I can add a View menu item and add two cascaded menu items beneath it: Align Left, and Align Right. Note the three new lines below: Line 17, 18, and 19.

def createMenu(self):
    # add menu header
    self.menuBar = QtWidgets.QMenuBar()
    self.fileMenu = self.menuBar.addMenu("File")
    self.aboutMenu = self.menuBar.addMenu("Help")
    
    # create menu items
    self.actionOpenFile = QtWidgets.QAction("Open File", self)
    self.actionSaveFile = QtWidgets.QAction("Save", self)
    self.actionQuit = QtWidgets.QAction("Quit", self)
    self.actionAbout = QtWidgets.QAction("Info", self)
       
    # bind menu item to menu header
    self.fileMenu.addAction(self.actionOpenFile)
    self.fileMenu.addAction(self.actionSaveFile)

    self.subFileMenuView = self.fileMenu.addMenu("View")
    self.subFileMenuView.addAction(self.actionAlignLeft)
    self.subFileMenuView.addAction(self.actionAlignRight)
    
    self.fileMenu.addAction(self.actionQuit)
    self.aboutMenu.addAction(self.actionAbout)

def createLayout(self):
    mainLayout = QtWidgets.QVBoxLayout()
    mainLayout.setMenuBar(self.menuBar)
    mainLayout.addWidget(self.labelName)
    mainLayout.addWidget(self.editText)
    mainLayout.addWidget(self.buttonTextUpdate)
    self.setLayout(mainLayout)

You can also set the cascaded menu items with a check symbol. Simply set the property of the QAction as such:

actionGroupView = QtWidgets.QActionGroup(self)
self.actionAlignLeft = QtWidgets.QAction("Align Left", actionGroupView)
self.actionAlignRight = QtWidgets.QAction("Align Right", actionGroupView)
self.actionAlignLeft.setCheckable(True)
self.actionAlignLeft.setChecked(True)
self.actionAlignRight.setCheckable(True)
actionGroupView.setExclusive(True)

Final Code – PySide2: The Menu Bar

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PySide2 import QtWidgets


class MainWindow(QtWidgets.QWidget):
    
    def __init__(self, *args):
        QtWidgets.QWidget.__init__(self, *args)
        self.createMenu()
        self.createComponents()
        self.createLayout()
        self.createConnects()
        self.setWindowTitle(self.tr("Name"))
		
    def createMenu(self):
	      # add menu header
        self.menuBar = QtWidgets.QMenuBar()
        self.fileMenu = self.menuBar.addMenu("File")
        self.aboutMenu = self.menuBar.addMenu("Help")
		
        # create menu items
        self.actionOpenFile = QtWidgets.QAction("Open File", self)
        self.actionSaveFile = QtWidgets.QAction("Save", self)
        self.actionQuit = QtWidgets.QAction("Quit", self)
        self.actionAbout = QtWidgets.QAction("Info", self)
        actionGroupView = QtWidgets.QActionGroup(self)
        self.actionAlignLeft = QtWidgets.QAction("Align Left", actionGroupView)
        self.actionAlignRight = QtWidgets.QAction("Align Right", actionGroupView)
        self.actionAlignLeft.setCheckable(True)
        self.actionAlignLeft.setChecked(True)
        self.actionAlignRight.setCheckable(True)
        actionGroupView.setExclusive(True)
        
	      # bind menu item to menu header
        self.fileMenu.addAction(self.actionOpenFile)
        self.fileMenu.addAction(self.actionSaveFile)
        self.subFileMenuView = self.fileMenu.addMenu("View")
        self.subFileMenuView.addAction(self.actionAlignLeft)
        self.subFileMenuView.addAction(self.actionAlignRight)
        self.fileMenu.addAction(self.actionQuit)
        self.aboutMenu.addAction(self.actionAbout)
		
    def createLayout(self):
        mainLayout = QtWidgets.QVBoxLayout()
        mainLayout.setMenuBar(self.menuBar)
        mainLayout.addWidget(self.labelName)
        mainLayout.addWidget(self.editText)
        mainLayout.addWidget(self.buttonTextUpdate)
        self.setLayout(mainLayout)
        
    def createComponents(self):
        self.labelName = QtWidgets.QLabel("Name")
        self.buttonTextUpdate = QtWidgets.QPushButton("Update")
        self.editText = QtWidgets.QLineEdit()
        
    def createConnects(self):
        pass

def main(argv):
    app = QtWidgets.QApplication(argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main(sys.argv)

Leave a comment

Your email address will not be published. Required fields are marked *