How to display images inside the new tab in Python
<p>Suppose that I have 10 images in a directory, and need them to be displayed inside a newly created tab once the user presses a push-button. The images must be displayed according to a 5x5 grid with a proper image label. </p>

<p>I have designed a GUI using Qt Designer containing three different tabs (tab1, tab2 and tab3), but it seems like this will not work for me - the images wouldn't display inside tab3. How can I make it done by using command-line coding rather than using Qt Designer in order to display all the images?</p>

<p>Below is the function to display the images:</p>

<pre><code># Display image at 'Tab 3' once user press button 'Display'
def display_result(self):

    # Get wafer qty
    qty = int(self.qty_selected)
    print('qty -&gt; ' + str(qty))

    # Get lot id
    name = str(self.lotId)
    print ('lotId -&gt; ' + str(name))

    for index in range(int(qty)):   
        pixmap = QtGui.QPixmap()
        path = r'c:\Users\mohd_faizal4\Desktop\Python\Testing\%s\%s_%d.jpeg' % (name, name, index + 1)
        print('load (%s) %r' % (pixmap.load(path), path))
        item = QtGui.QListWidgetItem(os.path.basename(path))
        item.setIcon(QtGui.QIcon(path))
        self.viewer.addItem(item)


    contents = QtGui.QWidget(self.tabWidget)
    layout = QtGui.QVBoxLayout(contents)
    self.tabWidget.addTab(contents, 'Tab 4')
</code></pre>

<p>I would like to display all the images inside 'Tab 4', instead of opening a another window.</p>

<p><a href="https://i.stack.imgur.com/TcMm3.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/TcMm3.jpg" alt="tab 3"></a>      </p>

<p>You need to create a new list-widget for each tab. Here's a demo:</p>

<pre><code>import sys, os
from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.tabs = QtGui.QTabWidget(self)
        self.edit = QtGui.QLineEdit(self)
        self.button = QtGui.QPushButton('New Tab', self)
        self.button.clicked.connect(self.createNewTab)
        layout = QtGui.QGridLayout(self)
        layout.addWidget(self.tabs, 0, 0, 1, 2)
        layout.addWidget(self.edit, 1, 0)
        layout.addWidget(self.button, 1, 1)

    def createNewTab(self):
        viewer = QtGui.QListWidget(self)
        viewer.setViewMode(QtGui.QListView.IconMode)
        viewer.setIconSize(QtCore.QSize(256, 256))
        viewer.setResizeMode(QtGui.QListView.Adjust)
        viewer.setSpacing(10)
        name = self.edit.text()
        for index in range(5):
            pixmap = QtGui.QPixmap()
            path = r'c:\Users\mohd_faizal4\Desktop\Python\Testing\%s\%s_%d.jpeg' % (name, name, index + 1)
            print('load (%s) %r' % (pixmap.load(path), path))
            item = QtGui.QListWidgetItem(os.path.basename(path))
            item.setIcon(QtGui.QIcon(path))
            viewer.addItem(item)
        index = self.tabs.count()
        self.tabs.addTab(viewer, 'Tab%d' % (index + 1))
        self.tabs.setCurrentIndex(index)

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(800, 150, 650, 500)
    window.show()
    sys.exit(app.exec_())
</code></pre>

python image user-interface tabs pyqt