PyQt5 tabs


In this article you will learn to use tabs with PyQt5. We’ll show the full code first, then explain. PyQt5 has a widget to create tabs known as QTabWidget. The QTabWidget can contain tabs (QWidgets), which have widgets on them such as labels, buttons, images etcetera.

pyqt5-tabs

Related course:
PyQt Desktop Apps with Python

PyQt5 tabs example
Full PyQt5 tabs example:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget,QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
 
class App(QMainWindow):
 
def __init__(self):
super().__init__()
self.title = 'PyQt5 tabs - pythonspot.com'
self.left = 0
self.top = 0
self.width = 300
self.height = 200
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
 
self.table_widget = MyTableWidget(self)
self.setCentralWidget(self.table_widget)
 
self.show()
 
class MyTableWidget(QWidget):
 
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
 
# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tabs.resize(300,200)
 
# Add tabs
self.tabs.addTab(self.tab1,"Tab 1")
self.tabs.addTab(self.tab2,"Tab 2")
 
# Create first tab
self.tab1.layout = QVBoxLayout(self)
self.pushButton1 = QPushButton("PyQt5 button")
self.tab1.layout.addWidget(self.pushButton1)
self.tab1.setLayout(self.tab1.layout)
 
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
 
@pyqtSlot()
def on_click(self):
print("\n")
for currentQTableWidgetItem in self.tableWidget.selectedItems():
print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())
 
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

Explanation:

To add a table to a window, we create a new class:

class MyTableWidget(QWidget)

We initialize the tab screen by creating a QTabWidget and two QWidgets for tabs.

self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tabs.resize(300,200)

We then add these tabs to the tab widget:

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

The contents of a tab are created using:

self.tab1.layout = QVBoxLayout(self)
self.pushButton1 = QPushButton("PyQt5 button")
self.tab1.layout.addWidget(self.pushButton1)
self.tab1.setLayout(self.tab1.layout)

Finally we add the tabs to the widget:

self.layout.addWidget(self.tabs)
self.setLayout(self.layout)

don’t forget the to add your custom tab widget to the window:

self.table_widget = MyTableWidget(self)
self.setCentralWidget(self.table_widget)
Back
Next

5 thoughts on “PyQt5 tabs

  1. arun soman - January 17, 2018

    Hi,
    How can I reduce the length of pyQt5 button . Now I am getting full 300,200 size. I want to reduce the size by one third of the original size.

    1. arun soman - January 18, 2018

      I got it which is nothing but self.tab1.layout = QHBoxLayout() instead of QVBoxLayout() .But all the buttons are in horizontal line. I want some buttons are in next line too any idea how to do it???

      1. arun soman - January 18, 2018

        Solved issue using self.tab2.layout = QGridLayout()

  2. Ken Du - December 31, 2017

    I got an error inside the definition of MyTableWidget class:
    self.layout = QVBoxLayout(self)

    >>>QLayout: Attempting to add QLayout “” to MyTableWidget “”, which already has a layout

    Get rid off the self in the parentheses shows no error, but why?

    1. Frank - January 5, 2018

      Probably it uses self even if not specified.

Leave a Reply