Exploring Tab Widgets in PySide6 (Part 4) | PyShine

Exploring Tab Widgets in PySide6 (Part 4)

 · 4 mins read

In this tutorial, we’ll delve into the usage of tab widgets in PySide6 to create a multi-tabbed graphical user interface (GUI). Tab widgets allow us to organize content into multiple tabs, making it easier for users to navigate between different sections of an application. We’ll build a simple example application to demonstrate how to create and customize tab widgets in PySide6.

Prerequisites

Before we begin, ensure you have Python and PySide6 installed on your system. You can install PySide6 using pip:

pip install PySide6

Designing the GUI

Our GUI will consist of a main window with a tab widget containing multiple tabs, each with its own content.

tab_widget_app.py

import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QTabWidget, QWidget, QLabel

class TabWidgetApp(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Tab Widget Demo')
        self.setGeometry(100, 100, 400, 300)

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        self.layout = QVBoxLayout(self.central_widget)

        self.tab_widget = QTabWidget()
        self.tab1 = QWidget()
        self.tab2 = QWidget()
        self.tab3 = QWidget()

        self.tab_widget.addTab(self.tab1, "Tab 1")
        self.tab_widget.addTab(self.tab2, "Tab 2")
        self.tab_widget.addTab(self.tab3, "Tab 3")

        self.layout.addWidget(self.tab_widget)

        self.initTab1()
        self.initTab2()
        self.initTab3()

    def initTab1(self):
        layout = QVBoxLayout(self.tab1)
        label = QLabel("Content of Tab 1")
        layout.addWidget(label)

    def initTab2(self):
        layout = QVBoxLayout(self.tab2)
        label = QLabel("Content of Tab 2")
        layout.addWidget(label)

    def initTab3(self):
        layout = QVBoxLayout(self.tab3)
        label = QLabel("Content of Tab 3")
        layout.addWidget(label)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    tabWidgetApp = TabWidgetApp()
    tabWidgetApp.show()
    sys.exit(app.exec())

Understanding the Code

  • We create a class TabWidgetApp inheriting from QMainWindow.
  • In the initUI method, we set up the main window and create a central widget.
  • We create a QTabWidget and add three tabs to it.
  • Each tab is represented by a QWidget.
  • We use the addTab method to add tabs to the tab widget.
  • We initialize the content of each tab using separate methods (initTab1, initTab2, initTab3).

Running the Application

Save the code to a file (e.g., tab_widget_app.py) and execute it using Python:

python tab_widget_app.py

You should see the GUI window with the tab widget containing three tabs, each displaying its own content.

Conclusion

In this tutorial, we explored how to create and customize tab widgets in PySide6. Tab widgets are a convenient way to organize content into multiple tabs, making it easier for users to navigate through different sections of an application. You can further enhance this application by adding more tabs, incorporating different widgets and layouts within each tab, or styling the GUI components to match your design preferences. Experiment with the code and explore the possibilities of PySide6 to create even more versatile and user-friendly GUI applications!