Create a custom widget class that does all the setup in its __init__
:
class ImageViewTab(QtGui.QWidget):
def __init__(self, parent=None)
super(ImageViewTab, self).__init__(parent)
layout = QtGui.QVBoxLayout(self)
self.imageview = pg.ImageView(self)
layout.addWidget(self.imageview)
then add instances of your widget to the tab-widget in a loop:
tab_widget = QtGui.QTabWidget()
for index in range(1, 11):
widget = ImageViewTab(tab_widget)
tab_widget.addTab(widget, 'Grid %s' % index)
After all the setup is done, you can use the QTabWidget.widget method access the widgets by index:
widget = tab_widget.widget(4)
widget.imageview.setImage(img)
The main benefit of this approach is that it is very flexible. You can add specialized methods to your custom class, create and emit custom signals, add slots, etc.