python logo

PyQt5 menu

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

To create a menu for a PyQt5 program we need to use a QMainWindow.
This type of menu is visible in many applications and shows right below the window bar. It usually has a file and edit sub menu.

Related course:
Create GUI Apps with PyQt5

The top menu can be created with the method menuBar().
Sub menus are added with addMenu(name)
Example:


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')

Individual buttons can be added to the menus like this:


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

PyQt5 menu example

Full code:


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 menu - 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('Exit application')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)

self.show()

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

Result:
pyqt5-menu

If you are new to programming Python PyQt, I highly recommend this book.

Download PyQT5 Examples

BackNext





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.