How to show a Tab (QWidget object) embedded in a QtWidget on button click?



  • Hello guys! I need a few help.

    Here is my design with Qt Design and PyQt5.
    0_1567676379725_Capture1.PNG

    Now I want to show the tab "Onglet i" when I click on it's corresponding push button "Ouvrir_Onglet_i".

    Below is my code that is not working. Three different ways tried under each button.

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_TabWidget(object):
        def setupUi(self, TabWidget):
            TabWidget.setObjectName("TabWidget")
            TabWidget.resize(400, 300)
            self.tab_1 = QtWidgets.QWidget()
            self.tab_1.setObjectName("tab_1")
    		
            self.Ouvrir_Onglet_1 = QtWidgets.QPushButton(self.tab_1)
            self.Ouvrir_Onglet_1.setGeometry(QtCore.QRect(10, 20, 91, 23))
            self.Ouvrir_Onglet_1.setObjectName("Ouvrir_Onglet_1")
    		
            self.Ouvrir_Onglet_2 = QtWidgets.QPushButton(self.tab_1)
            self.Ouvrir_Onglet_2.setGeometry(QtCore.QRect(10, 50, 91, 23))
            self.Ouvrir_Onglet_2.setObjectName("Ouvrir_Onglet_2")
    		
            self.Ouvrir_Onglet_3 = QtWidgets.QPushButton(self.tab_1)
            self.Ouvrir_Onglet_3.setGeometry(QtCore.QRect(10, 80, 91, 23))
            self.Ouvrir_Onglet_3.setObjectName("Ouvrir_Onglet_3")
    		
            TabWidget.addTab(self.tab_1, "")
    		
            self.tab_2 = QtWidgets.QWidget()
            self.tab_2.setObjectName("tab_2")
            TabWidget.addTab(self.tab_2, "")
    		
            self.tab_3 = QtWidgets.QWidget()
            self.tab_3.setObjectName("tab_3")
            TabWidget.addTab(self.tab_3, "")
    		
            self.tab_4 = QtWidgets.QWidget()
            self.tab_4.setObjectName("tab_4")
            TabWidget.addTab(self.tab_4, "")
    
            self.retranslateUi(TabWidget)
            TabWidget.setCurrentIndex(0)
            QtCore.QMetaObject.connectSlotsByName(TabWidget)
    		
        def Fonction_Ouvrir_Onglet_1(self):
            self.window = QtWidgets()
            self.ui = Ui_TabWidget()
            self.ui.setupUi(self.window.tab_2)
            self.window.tab_2.show()
    		
            self.Ouvrir_Onglet_1.clicked.connect(self.Fonction_Ouvrir_Onglet_1)
    		
        def Fonction_Ouvrir_Onglet_2(self):
            self.Ui_TabWidget.setCurrentIndex(2)
    
            self.Ouvrir_Onglet_2.clicked.connect(self.Fonction_Ouvrir_Onglet_2)
    		
            self.Ouvrir_Onglet_3.clicked.connect(self.TabWidget.setCurrentIndex(3))
    
    
        def retranslateUi(self, TabWidget):
            _translate = QtCore.QCoreApplication.translate
            TabWidget.setWindowTitle(_translate("TabWidget", "TabWidget"))
            self.Ouvrir_Onglet_1.setText(_translate("TabWidget", "Ouvrir_Onglet_1"))
            self.Ouvrir_Onglet_2.setText(_translate("TabWidget", "Ouvrir_Onglet_2"))
            self.Ouvrir_Onglet_3.setText(_translate("TabWidget", "Ouvrir_Onglet_3"))
            TabWidget.setTabText(TabWidget.indexOf(self.tab_1), _translate("TabWidget", "Menu_principal"))
            TabWidget.setTabText(TabWidget.indexOf(self.tab_2), _translate("TabWidget", "Onglet 1"))
            TabWidget.setTabText(TabWidget.indexOf(self.tab_3), _translate("TabWidget", "Onglet 2"))
            TabWidget.setTabText(TabWidget.indexOf(self.tab_4), _translate("TabWidget", "Onlget 3"))
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        TabWidget = QtWidgets.QTabWidget()
        ui = Ui_TabWidget()
        ui.setupUi(TabWidget)
        TabWidget.show()
        sys.exit(app.exec_())
    

    Note : I generated the code using "pyiuc5" in command, then tried to do job with functions Fonction_Ouvrir_Onglet_1, Fonction_Ouvrir_Onglet_2 and self.TabWidget.setCurrentIndex() method. None of these 3 attempts is working.

    Eternal gratitude to my helper.
    Thanks in advance



  • Okay I do not think you really want to put your Buttons on a Tab as such I have rendered a version where the Tabs are separate from the Buttons -- not sure why you are using Buttons though since clicking on the Tab itself brings it into focus?

    from sys import exit as sysExit
    
    from PyQt5.QtCore    import Qt
    from PyQt5.QtGui     import QFont
    # QWidgets Container Objects
    from PyQt5.QtWidgets import QApplication, QWidget, QTabWidget
    from PyQt5.QtWidgets import QVBoxLayout, QHBoxLayout, QGroupBox
    # QWidgets Action Objects
    from PyQt5.QtWidgets import QPushButton
    
    class Onglets(QTabWidget):
        def __init__(self):
            QTabWidget.__init__(self)
    
            self.Tab1 = QWidget()
            self.addTab(self.Tab1, 'Onglet 1')
    
            self.Tab2 = QWidget()
            self.addTab(self.Tab2, 'Onglet 2')
    
            self.Tab3 = QWidget()
            self.addTab(self.Tab3, 'Onglet 3')
    
    class MainWindow(QWidget):
        def __init__(self):
            QWidget.__init__(self)
            self.resize(400, 300)
            self.setWindowTitle('Ouvrir Onglets')
    
          # Left Side Area -----
            self.btnOng1 = QPushButton()
            self.btnOng1.setText('Ouvrir Onglet 1')
            self.btnOng1.clicked.connect(self.Fonction_Ouvrir_Onglet_1)
    
            self.btnOng2 = QPushButton()
            self.btnOng2.setText('Ouvrir Onglet 2')
            self.btnOng2.clicked.connect(self.Fonction_Ouvrir_Onglet_2)
    
            self.btnOng3 = QPushButton()
            self.btnOng3.setText('Ouvrir Onglet 3')
            self.btnOng3.clicked.connect(self.Fonction_Ouvrir_Onglet_3)
    
            vbxLeftBtns = QVBoxLayout()
            vbxLeftBtns.addWidget(self.btnOng1)
            vbxLeftBtns.addWidget(self.btnOng2)
            vbxLeftBtns.addWidget(self.btnOng3)
            vbxLeftBtns.addStretch(1)
            #-----
            font = QFont()
            font.setBold(True)
            gbxLeftSide = QGroupBox('Principle Menu')
            gbxLeftSide.setFont(font)
            gbxLeftSide.setAlignment(Qt.AlignHCenter )
            gbxLeftSide.setLayout(vbxLeftBtns)
            #-----
            self.OngletTabs = Onglets()
            #-----
            hbxAll = QHBoxLayout()
            hbxAll.addWidget(gbxLeftSide)
            hbxAll.addWidget(self.OngletTabs)
            
            self.setLayout(hbxAll)
    
        def Fonction_Ouvrir_Onglet_1(self):
            self.OngletTabs.setCurrentIndex(0)
    
        def Fonction_Ouvrir_Onglet_2(self):
            self.OngletTabs.setCurrentIndex(1)
    
        def Fonction_Ouvrir_Onglet_3(self):
            self.OngletTabs.setCurrentIndex(2)
    
    if __name__ == '__main__':
        MainThred = QApplication([])
    
        MainGui = MainWindow()
        MainGui.show()
    
        sysExit(MainThred.exec())
    


  • Hello @denni-0,

    Thak you very much for your answer. It's working perfectly and I have learned a lot through your coding style.

    The reasons for the buttons in a tab are :

    1. Other widgets are on the main menu windows
    2. I have a lot of tabs (42 I think, corresponding to 42 data bases).

    I want to be able to reach any of those tabs from that specific one by button click, and, come back there from any other tab by button click (that last button will be in the current tab I am in, redirecting to the tab containing all my buttons).

    I need to understand inheritance to make my tabs communicate that way. Added to what you have provided, I think it will do.

    Thanks at last!



  • With your description I would strongly suggest then doing away with the Tabs all together and use a Stackable Widget basically think an array of views or turning your tabs into an array and only showing the contents of the tab rather than showing the tab at all. If you need help on how to change this into a stackable widget just ask but give it a try first you will learn more from trying it than you will from being spoon fed ;)


 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.