PySide6 QTabWidget Example

QTabWidget

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# The QTabWidget class provides a stack of tabbed widgets.  :S
# ie. tabs

import sys

from PySide6.QtWidgets import (QApplication, QWidget,
    QVBoxLayout, QTabWidget, QRadioButton, QCheckBox)


class Window(QWidget):

    def __init__(self):

        super().__init__()

        layout = QVBoxLayout()
        self.setLayout(layout)

        # 1 - Create the tab widget

        self.tab_widget = QTabWidget()

        # 2 - Create some widgets
        #     A tab contains only one widget
        #     but that widget can be a QWidget instance
        #     or some other container.

        # Tab 0 widgets:

        styles = QWidget()
        styles_layout = QVBoxLayout()
        styles_layout.addWidget(QCheckBox('Heading'))
        styles_layout.addWidget(QCheckBox('Paragraph'))
        styles_layout.addWidget(QCheckBox('List'))
        styles.setLayout(styles_layout)

        # Tab 1 widgets

        margins = QWidget()
        margins_layout = QVBoxLayout()
        margins_layout.addWidget(QRadioButton('Normal'))
        margins_layout.addWidget(QRadioButton('Wide'))
        margins_layout.addWidget(QRadioButton('Narrow'))
        margins.setLayout(margins_layout)

        # 3 - Add tabs to the widget

        self.tab_widget.addTab(styles, 'Styles')
        self.tab_widget.addTab(margins, 'Margins')

        layout.addWidget(self.tab_widget)


if __name__ == '__main__':

    app = QApplication(sys.argv)

    main_window = Window()
    main_window.show()

    sys.exit(app.exec())

QTabWidget is a tabbed container widget - when you click on a tab its associated page is shown. To use it in your application

  1. Create a QTabWidget object.

  2. Create its intended child widgets. Each page contains a single widget but that widget, in turn, can be a QWidget or a container class which allows you to pack multiple children into a QTabWidget page.

  3. Use QTabWidget.addTab() to add the child widgets to the tab widget object. In the example we have two QWidgets and add each to the tab widget. The first QWidget has three check boxes as its children and the second QWidgets children are three radio buttons.

The tab indexes start with zero so the first tab has the index of 0 and the second has the index of 1. Each tab has a label (Styles and Margins in the example). You can change the tab position (North, South, West and East) and shape (Rounded and Triangular).