How to remove a particular tab in QTabWidget in PyQt5?

Viewed 34

How to remove a particular tab? Here is my Code, If I close the first tab(index:0), then the entire opened tab will be closed and show a blank window only. If I closed the second tab(index:1), then the first tab(index :1 ) and the second tabs( index : 2) tabs will be closed.

But I nedd, If I close the first tab(index:0), then that particular tab is only closed and the remaining opened tabs are active. How do achieve it?

import sys
from PyQt5.QtWidgets import *

class Contact(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Contact Details")
        self.layout = QFormLayout()
        self.layout.addRow("Name", QLineEdit())
        self.layout.addRow("Address", QLineEdit())
        self.setLayout(self.layout)

class Personal(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Personal Details")
        self.layout = QFormLayout()
        self.sex = QHBoxLayout()
        self.sex.addWidget(QRadioButton("Male"))
        self.sex.addWidget(QRadioButton("Female"))
        self.layout.addRow(QLabel("Sex"), self.sex)
        self.layout.addRow("Date of Birth", QLineEdit())
        self.setLayout(self.layout)

class Educational(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Educational Details")
        self.layout = QHBoxLayout()
        self.layout.addWidget(QLabel("subjects"))
        self.layout.addWidget(QCheckBox("Physics"))
        self.layout.addWidget(QCheckBox("Maths"))
        self.setLayout(self.layout)

class MainPage(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QTabWidget Samples")
        self.setGeometry(100,100,1800,400)
        self.subwindow = None
        self.Ui()
        self.show()

    def Ui(self):
        self.btn1=QPushButton("Contact")
        self.btn1.setFixedSize(100, 30)
        self.btn1.clicked.connect(self.contacttab)
        self.btn2 = QPushButton("personal")
        self.btn2.setFixedSize(100, 30)
        self.btn2.clicked.connect(self.personaltab)
        self.btn3 = QPushButton("Educational")
        self.btn3.setFixedSize(100, 30)
        self.btn3.clicked.connect(self.educationaltab)


        self.tab = QTabWidget()
        self.tab.setTabsClosable(True)
        self.tab.setTabShape(QTabWidget.Triangular)
        self.tab.setTabPosition(QTabWidget.North)

        self.left_layout = QVBoxLayout()
        self.right_layout = QHBoxLayout()
        self.main_layout = QHBoxLayout()

        self.left_layout.setContentsMargins(3,5,5,3)
        self.left_layout.addWidget(self.btn1)
        self.left_layout.addWidget(self.btn2)
        self.left_layout.addWidget(self.btn3)
        self.left_layout.addStretch()

        self.right_frame = QFrame()
        self.right_frame.setObjectName("rightframe")
        self.right_frame.setStyleSheet(f"QFrame#rightframe{{background-color: lightgreen;}}")
        self.right_frame.setLayout(self.right_layout)
        self.right_layout.addWidget(self.tab)

        self.main_layout.setSpacing(5)
        self.main_layout.setContentsMargins(0,0,0,0)
        self.main_layout.addLayout(self.left_layout)
        self.main_layout.addWidget(self.right_frame)
        self.main_layout.addStretch()
        self.setLayout(self.main_layout)

        self.contacttab = Contact()
        self.personaltab = Personal()
        self.educationaltab = Educational()

    def contacttab(self):
        self.tab.addTab(self.contacttab,"Contacts")
        self.tab.setCurrentWidget(self.contacttab)
        self.tab.tabCloseRequested.connect(self.closetab)

    def personaltab(self):
        self.tab.addTab(self.personaltab,"Personal")
        self.tab.setCurrentWidget(self.personaltab)
        self.tab.tabCloseRequested.connect(self.closetab)
    def educationaltab(self):
        self.tab.addTab(self.educationaltab,"Educational")
        self.tab.setCurrentWidget(self.educationaltab)
        self.tab.tabCloseRequested.connect(self.closetab)

    def closetab(self,index):
        print("index",index)
        self.tab.removeTab(index)
        

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainwindow = MainPage()
    app.setStyle("Windows")
    mainwindow.show()
    sys.exit(app.exec_())
0 Answers
Related