'How to show few images in few windows in PyQt5?

I've got a problem with my project. I'd like to show few images in few windows to make operation on them. I've got a problem with displaying, to wit: if I'm browsing first image, it's showing, correctly, in different window. But if I'm browsing another image, it's showing in different window too, but first image on first window disappeard. There is my code and picture with problem:

class MainWindow(QWidget):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Image project")
        self.setFixedWidth(500)
        self.setFixedHeight(50)
        self.photo = QLabel(self)
        btn = QPushButton('Browse')
        btn.clicked.connect(self.open_image)
        grid = QGridLayout(self)
        grid.addWidget(btn, 0, 0, Qt.AlignHCenter)
        self.popups = []

    def add_new_window(self, photo):
        self.window = PhotoWindow(photo)
        self.popups.append(self.window)
        self.window.show()

    def open_image(self, filename=None):
        if not filename:
            filename, _ = QFileDialog.getOpenFileName(
                                                      self, 'Select Photo',
                                                      QDir.currentPath(),
                                                      'Images (*.png *.jpg *.gif *.bmp)')
            if not filename:
                return
        self.photo.setPixmap(QPixmap(filename))
        self.add_new_window(self.photo)


class PhotoWindow(QWidget):
    def  __init__(self, photo):
        super(PhotoWindow, self).__init__()
        self.photo = photo
        grid = QGridLayout(self)
        grid.addWidget(self.photo, 0, 0)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = MainWindow()
    gui.show()
    sys.exit(app.exec_())

Screen with my problem: (https://i.stack.imgur.com/HBBTX.png)

Thank you in advance for all solutions.

EDIT

User ekhumoro found the solution. I had to re-create PhotoWindow class constructor to create new label. I threw up self.photo argument, as constructor parameter in PhotoWindow I took filename and create and set new QPixMap.

open_image:

    def open_image(self, filename=None):
        if not filename:
            filename, _ = QFileDialog.getOpenFileName(
                                                      self, 'Select Photo',
                                                      QDir.currentPath(),
                                                      'Images (*.png *.jpg *.gif *.bmp)')
            if not filename:
                return
        self.add_new_window(filename)

PhotoWindow:

class PhotoWindow(QWidget):
    def  __init__(self, photo):
        super(PhotoWindow, self).__init__()
        label = QLabel(self)
        pixmap = QPixmap(photo)
        label.setPixmap(pixmap)
        grid = QGridLayout(self)
        grid.addWidget(label, 0, 0)


Solution 1:[1]

I am not 100 percent sure, but i think with your add_new_window function you are overwriting your old window. A new window pops up with the new picture and the old one will be closed, as the window in self.window got overwritten, althoug you add it to the popups list. Try to make a local variable "window" in your add_new_window function instead of the self.window variable.

But as i say, i am not sure, it is just an idea ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saibot Robot