In this PyQt5 article i want to show you How To Create Context Menu In PyQt5. so first of all let’s talk about PyQt5.
What is PyQt5 ?
PyQt5 is Python binding for the Qt cross platform application framework and it allows developers to create desktop applications with graphical user interface (GUI) using Python programming language. PyQt5 is developed by Riverbank Computing and is licensed under the GPL and commercial licenses.
Qt is popular framework for developing graphical user interfaces, and PyQt5 provides access to all of Qt’s functionality, including support for widgets, layouts, graphics, multimedia and networking and it also provides Pythonic API for working with Qt that makes it easy to create and manage GUIs in Python.
Some of the key features of PyQt5 include:
- Cross-platform support: PyQt5 allows developers to create applications that run on multiple platforms, including Windows, macOS and Linux.
- Qt Designer integration: PyQt5 includes integration with Qt Designer, Qt Designer visual tool for designing and laying out GUIs.
- Extensive documentation: PyQt5 provides extensive documentation and examples, that make it easy to learn and use.
- Support for modern Python features: PyQt5 supports the latest features of Python, such as type annotations, async/await and f-strings.
- Large community: PyQt5 has large and active community of developers and it provides support, guidance and contributions to the project.
PyQt5 is often used in desktop application development for creating GUIs with different functionality, such as data visualization, multimedia playback and database integration. it is also often used in scientific and engineering applications for creating custom visualization and analysis tools.
You can use pip for the installation of PyQt5.
1 |
pip install PyQt5 |
How To Create Context Menu In PyQt5
Menu widget can be either pull down menu in a menu bar or a standalone context menu. Pull down menus are shown by the menu bar when the user clicks on the respective item or presses the specified shortcut key. Context menus are usually invoked by some special keyboard key or by right-clicking.
Also you can read more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Psyide2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
First we need some imports.
1 2 3 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu import sys |
After that we are going to create our main Window class that extends from QMainWindow and in that class we have initialized basic requirements of our window . also we have called our InitWindow() method in here.
1 2 3 4 5 6 7 8 9 |
class Window(QMainWindow): def __init__(self): super().__init__() self.title = "PyQt5 Context Menu" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() |
OK now in here we are going to set our window icon, window title and also window geometry.
1 2 3 4 5 |
def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.show() |
Now we create our contextMenuEvent that is a built in method in PyQt5.
1 2 3 4 5 6 7 8 |
def contextMenuEvent(self, event): contextMenu = QMenu(self) newAct = contextMenu.addAction("New") openAct = contextMenu.addAction("Open") quitAct = contextMenu.addAction("Quit") action = contextMenu.exec_(self.mapToGlobal(event.pos())) if action == quitAct: self.close() |
Also every PyQt5 application must create an application object.
1 |
App = QApplication(sys.argv) |
Finally, we enter the main loop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec()) |
Complete source code for How To Create Context Menu In PyQt5
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 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu import sys class Window(QMainWindow): def __init__(self): super().__init__() self.title = "PyQt5 Context Menu" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.show() def contextMenuEvent(self, event): contextMenu = QMenu(self) newAct = contextMenu.addAction("New") openAct = contextMenu.addAction("Open") quitAct = contextMenu.addAction("Quit") action = contextMenu.exec_(self.mapToGlobal(event.pos())) if action == quitAct: self.close() App = QApplication(sys.argv) window = Window() sys.exit(App.exec()) |
Run complete code and you will see this output.
Also you can watch the complete video for this article .
Subscribe and Get Free Video Courses & Articles in your Email
1 thought on “How To Create Context Menu In PyQt5”