1. Simple menu with QMenuBar PyQt5
To create a menu, the PyQt5 library has a class named QMenuBar which allows you to create a menu bar. By making an instantiation on the QMenuBar class, we create a menuBar object with the methods:
- addMenu(): allowing to add a menu like File , Edit, Option...
- addAction(): allowing to add commands to menu items like: File->Open , File->Save , File->Save As , File->Exit,...
- addSeparator(): allowing to add a separator between the menu elements
Example
from PyQt5.QtWidgets import QApplication, QWidget,QMenuBar
import sys
app = QApplication(sys.argv)
root = QWidget()
root.setWindowTitle("Simple QMenu PyQt5")
root.setGeometry(100 , 100 , 500 , 300)
# create a Menubar by instanciating the QMenuBar class
menuBar = QMenuBar(root)
menuBar.setGeometry(0,0, 500, 25)
# create a menu called 'File'
File = menuBar.addMenu('File')
# add actions to the menu 'File'
File.addAction('New')
File.addAction('Open' )
File.addAction('Save')
File.addAction('Save As')
File.addAction('Exit')
root.show()
sys.exit(app.exec_())

Remark
We can also add command action to the action menu for example when we click on the File->Open, a new window appears, when we click on the File->Exit we exit the window...
Example
from PyQt5.QtWidgets import QApplication, QWidget,QMenuBar, QAction
import sys , os
def exit():
root.close()
def new():
os.popen("python simplemenu.py")
app = QApplication(sys.argv)
root = QWidget()
root.setWindowTitle("Simple QMenu PyQt5")
root.setGeometry(100 , 100 , 500 , 300)
# create a Menubar by instanciating the QMenuBar class
menuBar = QMenuBar(root)
menuBar.setGeometry(0,0, 500, 25)
# create a menu called 'File'
File = menuBar.addMenu('File')
# add actions to the menu 'File'
File.addAction('New' , new)
File.addAction('Open' )
File.addAction('Save')
File.addAction('Save As')
File.addAction('Exit' , exit)
root.show()
sys.exit(app.exec_())
2. QMenuBar according to the object approach PyQt5
In order to write a clean code, we can also write previous code by using the python class:
from PyQt5.QtWidgets import QApplication, QWidget,QMenuBar
import sys, os
class MyWindow(QWidget):
def __init__(self, win):
super().__init__()
self.win = win
def exit(self):
self.win.close()
def new(self):
os.popen("python object1.py")
def build(self):
self.win.setWindowTitle("Example QMenu PyQt5")
self.win.setGeometry(100 , 100 , 500 , 300)
self.menubar = QMenuBar(self.win)
self.File = self.menubar.addMenu('File')
self.File.addAction('New' , self.new)
self.File.addAction('Exit' , self.exit)
if __name__ == '__main__':
app = QApplication(sys.argv)
root = QWidget()
mywin = MyWindow(root)
mywin.build()
root.show()
sys.exit(app.exec_())
3. Add an action to a menu item using the QAction class
Now we will see another method of adding an action to a menu item using the QAction class. For this purpose the following steps must be followed:
- we create the action: using QAction class
- We add the action: to the menu using the addAction() method
- We connect the action: to the method that performs the action using the triggered.connect() method
# create an action with QAction class
exitAction = QAction('&Exit' , self)
# adding action to the menu File
self.File.addAction(exitAction)
# adding a shrtcut to the action
exitAction.setShortcut('Ctrl+Q')
# connecting to the method that perform the action
exitAction.triggered.connect(self.exit)
Full code
from PyQt5.QtWidgets import QApplication, QWidget,QMenuBar, QAction
import sys, os
class MyWindow(QWidget):
def __init__(self, win):
super().__init__()
self.win = win
def exit(self):
self.win.close()
def new(self):
os.popen("python object1.py")
def build(self):
self.win.setWindowTitle("Example QMenu PyQt5")
self.win.setGeometry(100 , 100 , 500 , 300)
self.menubar = QMenuBar(self.win)
self.File = self.menubar.addMenu('File')
# create an action with QAction class
exitAction = QAction('&Exit' , self)
# adding action to the menu File
self.File.addAction(exitAction)
# adding a shrtcut to the action
exitAction.setShortcut('Ctrl+Q')
# connecting to the method that perform the action
exitAction.triggered.connect(self.exit)
if __name__ == '__main__':
app = QApplication(sys.argv)
root = QWidget()
mywin = MyWindow(root)
mywin.build()
root.show()
sys.exit(app.exec_())
Younes Derfoufi
my-courses.net