I would like, dynamically, create tabs in a TabWidget and in each tab add a widget (for a graphic) and a button.
So far I can create tabs and rename them based on a list but I cannot figure out how to "select" each tab one by one in order to add a widget and a button. I guess I should use an indexation to point out the tab I can't find any article explaining how to do that Note: the first tab already has a widget and a button (added via QtDesigner).
Here my code so far:
Frontend (developed using QtDesigner):
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(447, 356)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_3 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_3.setObjectName("gridLayout_3")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.gridLayout_2 = QtWidgets.QGridLayout(self.tab)
self.gridLayout_2.setObjectName("gridLayout_2")
self.widget = QtWidgets.QWidget(self.tab)
self.widget.setObjectName("widget")
self.gridLayout_2.addWidget(self.widget, 1, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.tab)
self.pushButton.setObjectName("pushButton")
self.gridLayout_2.addWidget(self.pushButton, 2, 0, 1, 1)
self.tabWidget.addTab(self.tab, "")
self.gridLayout_3.addWidget(self.tabWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 447, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Backend:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QMainWindow, QWidget, QApplication
from test import Ui_MainWindow
class Ui_MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
self.setupUi(self)
self.list = ['a', 'b', 'c', 'd']
self.populate()
def populate(self):
for i in range(0, len(self.list)):
if i > 0:
self.tab = QtWidgets.QWidget()
self.tabWidget.addTab(self.tab, self.list[i])
self.tabWidget.setTabText(i, self.list[i])
self.grid = QtWidgets.QGridLayout()
self.btn = QtWidgets.QPushButton()
self.widget = QtWidgets.QWidget()
self.grid.addWidget(self.widget, 0, 0, 1, 1)
self.grid.addWidget(self.btn, 1, 0, 1, 1)
if __name__ == '__main__':
app = QApplication(sys.argv)
prog = Ui_MainWindow()
prog.showMaximized()
sys.exit(app.exec_())