python logo

pyqt5 menu


Python hosting: Host, run, and code Python in the cloud!

To design a menu for a PyQt5 application, utilizing QMainWindow is essential.
This type of menu can be observed in many applications and it displays right beneath the window bar, typically comprising of sub-menus like ‘file’ and ‘edit’.

Essential Course Recommendation:
Master GUI Applications with PyQt5

The primary menu in PyQt5 can be constructed using the menuBar() method.
Sub-menus can be effortlessly integrated using the addMenu(name) function.
Here’s a simple demonstration:

mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
editMenu = mainMenu.addMenu('Edit')
viewMenu = mainMenu.addMenu('View')
searchMenu = mainMenu.addMenu('Search')
toolsMenu = mainMenu.addMenu('Tools')
helpMenu = mainMenu.addMenu('Help')

Furthermore, individual menu items or buttons can be incorporated into these menus.
Below is an example of how to integrate an ‘Exit’ button:

exitButton = QAction(QIcon('exit24.png'), 'Exit', self)
exitButton.setShortcut('Ctrl+Q')
exitButton.setStatusTip('Exit the software')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)

The button action is then connected using a pyqtslot.

Complete PyQt5 Menu Illustration:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QMainWindow):

def __init__(self):
super().__init__()
self.title = 'PyQt5 interactive menu tutorial - pythonspot.com'
self.left = 10
self.top = 10
self.width = 640
self.height = 400
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
editMenu = mainMenu.addMenu('Edit')
viewMenu = mainMenu.addMenu('View')
searchMenu = mainMenu.addMenu('Search')
toolsMenu = mainMenu.addMenu('Tools')
helpMenu = mainMenu.addMenu('Help')

exitButton = QAction(QIcon('exit24.png'), 'Exit', self)
exitButton.setShortcut('Ctrl+Q')
exitButton.setStatusTip('Terminate the program')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)

self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

Output:
PyQt5 Menu Demonstration

Are you excited to explore more?
Download Comprehensive PyQT5 Examples
Navigate: [Previous] [Next]






Leave a Reply:




Stuart Walsh Wed, 14 Jun 2017

We are trying to teach our students at High School level - Year 12, PyQt5 with Python. They have been given the task of developing GUI's and
•Buttons well placed and easy to navigate
•Screen elements from tutorials
•Colour background on opening screen
•Instructions screen or embedded in the program
•Home button on each screen

Is it possible within PyQt5 to develop all those above to add to the game console? I know it must be, but while ok with Python code itself, PyQt5 is still a learning curve for teachers as well as students?

Any help would be very much appreciated.

Stuart Walsh

Frank Thu, 15 Jun 2017

Hi Stuart,

These widgets are supported by PyQt, you can use them with any Python program.
It sounds like you want a pyqt5 wizard or tabs.

What is the game console? If you have the code, you can add a PyQt5 GUI around it.