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:
1 - addMenu(): allowing to add a menu like File , Edit, Option...
2 - addAction(): allowing to add commands to menu items like: File->Open , File->Save , File->Save As , File->Exit,...
3 - 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_())