PyQt - QTab Widget



In GUI applications, a tab is a part of the user interface element which allows computer users to quickly switch between one or more sets of tabs(tasks).

If a form has too many fields to be displayed simultaneously, they can be arranged on different pages and placed under each tab of a Tabbed Widget.

The QTabWidget provides a tab bar and a page area. The page under the first tab is displayed and others are hidden. The user can view any page by clicking on the desired tab.

Following are some of the frequently methods uses for QTabWidget class −

Sr.No. Methods & Description
1

addTab()

Adds a tab associated with a widget page

2

insertTab()

Inserts a tab with the page at the desired position

3

removeTab()

Removes tab at given index

4

setCurrentIndex()

Sets the index of the currently visible page as current

5

setCurrentWidget()

Makes the visible page as current

6

setTabBar()

Sets the tab bar of the widget

7

setTabPosition()

Position of the tabs are controlled by the values

QTabWidget.North - above the pages

QTabWidget.South - below the pages

QTabWidget.West - to the left of the pages

QTabWidget.East - to the right of the pages

8

setTabText()

Defines the label associated with the tab index

The following signals are associated with QTabWidget object −

Sr.No. Methods & Description
1

currentChanged()

Whenever the current page index changes

2

tabClosedRequested()

When the close button on the tab is clicked

Example

In the following example, the contents of a form are grouped in three categories. Each group of widgets is displayed under a different tab.

Top level window itself is a QTabWidget. Here, we have added three tabs as a parameter to the method addTab().

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

Each tab displays a sub form designed using a layout. Tab text is altered by the statement.

self.setTabText(0, "Contact Details")
self.setTabText(1, "Personal Details")
self.setTabText(2, "Education Details")

Below is the complete code of QTabwidget that displays the tab more than once.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QTabWidget, QFormLayout, QLineEdit, QHBoxLayout, QRadioButton, QLabel, QCheckBox

class TabDemo(QTabWidget):
   def __init__(self, parent=None):
      super().__init__(parent)
      self.tab1 = QWidget()
      self.tab2 = QWidget()
      self.tab3 = QWidget()

      self.addTab(self.tab1, "Tab 1")
      self.addTab(self.tab2, "Tab 2")
      self.addTab(self.tab3, "Tab 3")
      self.tab1UI()
      self.tab2UI()
      self.tab3UI()
      self.setWindowTitle("Tab Demo")
   def tab1UI(self):
      layout = QFormLayout()
      layout.addRow("Name", QLineEdit())
      layout.addRow("Address", QLineEdit())
      self.setTabText(0, "Contact Details")
      self.tab1.setLayout(layout)
   def tab2UI(self):
      layout = QFormLayout()
      sex = QHBoxLayout()
      sex.addWidget(QRadioButton("Male"))
      sex.addWidget(QRadioButton("Female"))
      layout.addRow(QLabel("Sex"), sex)
      layout.addRow("Date of Birth", QLineEdit())
      self.setTabText(1, "Personal Details")
      self.tab2.setLayout(layout)
   def tab3UI(self):
      layout = QHBoxLayout()
      layout.addWidget(QLabel("Subjects"))
      layout.addWidget(QCheckBox("Physics"))
      layout.addWidget(QCheckBox("Maths"))
      self.setTabText(2, "Education Details")
      self.tab3.setLayout(layout)
def main():
   app = QApplication(sys.argv)
   ex = TabDemo()
   ex.show()
   sys.exit(app.exec())

if __name__ == '__main__':
   main()

Output

The above code produces the following output −

QTabWidget Output
Advertisements