Tabs can be added to a QTabWidget. The QTabWidget can be added to a layout and the layout to the window.

There can be any amount of tabs. The example below shows tabs added ta qt window.

tabs in pyqt window

image: tabs showing in a pyqt window.

Related Course: Create GUI Apps with Python PyQt5

Tab example

PyQt tabs example

Run the code below to see a tab widget in a pyqt window. Navigating between the tabs shows the widgets added to the tab.

To add a tab to a QTabWidget, call the method .addTab().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
layout = QGridLayout()
self.setLayout(layout)
label1 = QLabel("Widget in Tab 1.")
label2 = QLabel("Widget in Tab 2.")
tabwidget = QTabWidget()
tabwidget.addTab(label1, "Tab 1")
tabwidget.addTab(label2, "Tab 2")
layout.addWidget(tabwidget, 0, 0)

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