Menu with PyQt4

PyQT Menu pythonspot
PyQT Menu
PyQt4 menus appear in the top of the window bar. A menu gives the user control over the application and is often location in the top of the window.

The QMainWindow class creates the main application window. This class has a method named menuBar() which adds the title bar.

Menus can be added to the title bar using addMenu(). Inside each menu you can add a command using the addAction method.

PyQt4 menubar

This code will add a menu to your qt4 app:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtGui import *
# Create an PyQT4 application object.
a = QApplication(sys.argv)       
# The QWidget widget is the base class of all user interface objects in PyQt4.
w = QMainWindow()
# Set window size. 
w.resize(320, 240)
# Set window title  
w.setWindowTitle("Hello World!") 
# Create main menu
mainMenu = w.menuBar()
fileMenu = mainMenu.addMenu('&File')
# Add exit button
exitButton = QAction(QIcon('exit24.png'), 'Exit', w)
exitButton.setStatusTip('Exit application')
# Show window 

Download PyQT Code (Bulk Collection)

11 thoughts on “Menu with PyQt4

  1. Jacco Van Dorp - December 9, 2015

    I’m trying to add some custom menu options. I assumed I could use the slots as we use them for buttons, but it seems I was wrong. What I’m trying to do is to run a sub-process. Is it possible to give an example of how to run something like that from a menu option?

    1. Frank - December 10, 2015

      What do you mean by custom menu options? If you want a separate process you could start a thread

      1. Jacco Van Dorp - December 16, 2015

        I was slightly mishandling the slotting mechanism. I was trying to use:


        where I should have used:


        My apologies for the vague question. I had just started on QT, but now that I’ve got some 30-ish hours in it I agree that the questions wasn’t really clear.

        That all said, maybe the Lambda trick could be included somewhere? I’m auto-generating menu’s from the init functions of my classes, and it allows me to track where a certain command is coming from(generally, by passing the class object as argument). Other problems it could solve could be a lot simpler though.

  2. Morm - August 8, 2015

    I copy-paste it to Xcode – and it shows only the window and its title – the rest is empty – what can be the reason for that?

    1. Staff - August 10, 2015

      Hi, this may be an issue with QT specific to Mac OS. Unfortunately, I don’t have access to a mac

      1. Matt - October 11, 2015

        on OS X here, and this seems to mac specific.

    2. Joshua - August 21, 2015

      If w = QWidget() and not w = QMainWindow() the option to the menu will not be available.

      1. Frank - August 22, 2015

        Thanks Joshua!

    3. Choon - October 13, 2015

      I had a same issue on Mac and resolved by inserting menu.setNativeMenuBar(False) below of mainMenu = w.menuBar().

      1. Gardnm4 - November 7, 2015

        You forgot a word in your solution. The line should be:


        1. Frank - November 8, 2015