In this Python PyQt5 lesson we are going to learn about Python PyQt5 QMenu, so PyQt5 QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Now open your Qt Designer, you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create MainWindow. because when you want to create menus or status bars you need to use QMainWindow, now we want to create our menu and menu items.
This is the design.
After completing the design you need to save the .ui file, iam going to name it menu.ui, now copy the file and paste it in the Scripts folder of your Python installation, because we want to convert our ui file in to python file and for converting you need to use pyuic5 module. pyuic5 module is located in the Scripts folder of your Python installation, run this command for converting in your terminal.
1 |
pyuic5 menu.ui -o menu.py -x |
And This is the converted code also we have added some icons to the menu items.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# -*- coding: utf-8 -*- # Form implementation generated from # reading ui file 'menu.ui' # # Created by: PyQt5 UI code generator 5.15.1 # # WARNING: Any manual changes made to this # file will be lost when pyuic5 is # run again. Do not edit this file unless you # know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(597, 375) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 597, 21)) self.menubar.setObjectName("menubar") self.menuFile = QtWidgets.QMenu(self.menubar) self.menuFile.setObjectName("menuFile") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.actionNew = QtWidgets.QAction(MainWindow) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("images/new.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.actionNew.setIcon(icon) self.actionNew.setObjectName("actionNew") self.actionSave = QtWidgets.QAction(MainWindow) icon1 = QtGui.QIcon() icon1.addPixmap(QtGui.QPixmap("images/Save.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.actionSave.setIcon(icon1) self.actionSave.setObjectName("actionSave") self.actionCopy = QtWidgets.QAction(MainWindow) icon2 = QtGui.QIcon() icon2.addPixmap(QtGui.QPixmap("images/Copy.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.actionCopy.setIcon(icon2) self.actionCopy.setObjectName("actionCopy") self.actionPaste = QtWidgets.QAction(MainWindow) icon3 = QtGui.QIcon() icon3.addPixmap(QtGui.QPixmap("images/Paste.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.actionPaste.setIcon(icon3) self.actionPaste.setObjectName("actionPaste") self.actionExit = QtWidgets.QAction(MainWindow) icon4 = QtGui.QIcon() icon4.addPixmap(QtGui.QPixmap("images/Exit.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.actionExit.setIcon(icon4) self.actionExit.setObjectName("actionExit") self.menuFile.addAction(self.actionNew) self.menuFile.addAction(self.actionSave) self.menuFile.addSeparator() self.menuFile.addAction(self.actionCopy) self.menuFile.addAction(self.actionPaste) self.menuFile.addAction(self.actionExit) self.menubar.addAction(self.menuFile.menuAction()) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.menuFile.setTitle(_translate("MainWindow", "File")) self.actionNew.setText(_translate("MainWindow", "New")) self.actionSave.setText(_translate("MainWindow", "Save")) self.actionCopy.setText(_translate("MainWindow", "Copy")) self.actionPaste.setText(_translate("MainWindow", "Paste")) self.actionExit.setText(_translate("MainWindow", "Exit")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_()) |
Run the complete code and this is the result.
Creating PyQt QMenu with Coding
OK now we want to create our pyqt5 menu using coding, in the previous part we have used Qt Designer, in here we are not going to use Qt Designer. this is the complete source code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
from PyQt5.QtWidgets import QApplication, QMainWindow,\ QAction import sys from PyQt5.QtGui import QIcon class Window(QMainWindow): def __init__(self): super().__init__() #window requrements like geometry,icon and title self.setGeometry(200,200,400,200) self.setWindowTitle("PyQt5 Menu") self.setWindowIcon(QIcon("python.png")) self.create_menu() def create_menu(self): main_menu = self.menuBar() fileMenu = main_menu.addMenu("File") newAction = QAction(QIcon('images/new.png'), "New", self) newAction.setShortcut("Ctrl+N") fileMenu.addAction(newAction) saveAction = QAction(QIcon('images/save.png'), "Save", self) saveAction.setShortcut("Ctrl+S") fileMenu.addAction(saveAction) fileMenu.addSeparator() copyAction = QAction(QIcon('images/copy.png'), "Copy", self) copyAction.setShortcut("Ctrl+C") fileMenu.addAction(copyAction) pasteAction = QAction(QIcon('images/paste.png'), "Paste", self) pasteAction.setShortcut("Ctrl+P") fileMenu.addAction(pasteAction) exitAction = QAction(QIcon('images/exit.png'), "Exit", self) exitAction.triggered.connect(self.close_window) fileMenu.addAction(exitAction) def close_window(self): self.close() App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
These are our window requirements like window title, window icon, width and height of the window, x and y position of the window.
1 2 3 |
self.setGeometry(200,200,400,200) self.setWindowTitle("PyQt5 Menu") self.setWindowIcon(QIcon("python.png")) |
In here we have created menubar and we have added a File menu in the menubar.
1 2 |
main_menu = self.menuBar() fileMenu = main_menu.addMenu("File") |
This is for creating the menu item, and we can use QAction for this, also you can see that we have added icon for the menu item.
1 2 3 |
newAction = QAction(QIcon('images/new.png'), "New", self) newAction.setShortcut("Ctrl+N") fileMenu.addAction(newAction) |
Also you can use the triggered() signal of menu item for adding functionlaity to the menu item.
1 |
exitAction.triggered.connect(self.close_window) |
Run the complete code and this is the result.