A menubar can be added to a PyQt window. Its a horizontal bar with buttons items, typically file menu and others.

This example adds a menubar and textbox to a PyQt window. As shown in the screenshot below.

menubar in pyqt

Related Course: Create GUI Apps with Python PyQt5

PyQt Menubar

A menubar can be constructed with QMenuBar(). You can add menus like so .addMenu("File"). Then add actions to the menu so .addAction("Open").

The menubar has to be added to a layout, which is done with this line layout.addWidget(menubar, 0, 0).

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
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
layout = QGridLayout()
self.setLayout(layout)

# create menu
menubar = QMenuBar()
layout.addWidget(menubar, 0, 0)
actionFile = menubar.addMenu("File")
actionFile.addAction("New")
actionFile.addAction("Open")
actionFile.addAction("Save")
actionFile.addSeparator()
actionFile.addAction("Quit")
menubar.addMenu("Edit")
menubar.addMenu("View")
menubar.addMenu("Help")

# add textbox
tbox = QPlainTextEdit()
layout.addWidget(tbox, 1, 0)


app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())

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

Download Examples