Search code examples
pythonpyqt5qpixmapqfiledialog

PyQt5 application closes after choosing file on QFileDialog


I am trying to write an application on PyQt5 that opens an image through QFileDialog and displays it on the main window. This is the code:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtWidgets import (QWidget, QLabel, QHBoxLayout, QVBoxLayout,
                             QApplication, QPushButton, QSlider, 
                             QFileDialog, QAction)


class Example(QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        path = 'image.png'

        original_img_label = QLabel('Original')
        processed_img_label = QLabel('Processed')

        orig_img = QPixmap(path)
        global orig_lbl
        orig_lbl = QLabel(self)
        orig_lbl.setPixmap(orig_img)
        proc_img = QPixmap(path)
        proc_lbl = QLabel(self)
        proc_lbl.setPixmap(proc_img)

        select_image_btn = QPushButton('Select image')
        select_image_btn.clicked.connect(self.getImage)
        clean_image_btn = QPushButton('Clean image')

        thresh1_sld = QSlider(Qt.Horizontal, self)
        thresh2_sld = QSlider(Qt.Horizontal, self)

        v_orig_lay = QVBoxLayout()
        v_orig_lay.addWidget(original_img_label)
        v_orig_lay.addWidget(orig_lbl)

        v_proc_lay = QVBoxLayout()
        v_proc_lay.addWidget(processed_img_label)
        v_proc_lay.addWidget(proc_lbl)


        h_img_lay = QHBoxLayout()
        h_img_lay.addStretch(1)
        h_img_lay.addLayout(v_orig_lay)
        h_img_lay.addStretch(1)
        h_img_lay.addLayout(v_proc_lay)
        h_img_lay.addStretch(1)

        h_btn_lay = QHBoxLayout()
        h_btn_lay.addStretch(1)
        h_btn_lay.addWidget(select_image_btn)
        h_btn_lay.addWidget(clean_image_btn)
        h_btn_lay.addStretch(1)

        v_main_lay = QVBoxLayout()
        v_main_lay.addLayout(h_img_lay)
        v_main_lay.addStretch(1)
        v_main_lay.addWidget(thresh1_sld)
        v_main_lay.addWidget(thresh2_sld)
        v_main_lay.addLayout(h_btn_lay)

        self.setLayout(v_main_lay)

        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Review')
        self.show()

    def getImage(self):

        filter = "Images (*.png *.jpg)"
        image_obj = QFileDialog.getOpenFileName(self, 'Open image', 'Desktop', filter)
        self.orig_lbl.setPixmap(QPixmap(image_obj))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

The getImage function is called by the select_image_btn button, wish should put the image_obj (image path) on the orig_lbl through setPixmap.

Everything goes fine till I select an image and click OK on the FileDialog. When I do this, the application shuts down.


Solution

  • Using global variables is a bad programming practice, in a class it is better to use the variable as an attribute. You Must change orig_lbl to self.orig_lbl.

    Another error you have is that the QFileDialog.getOpenFileName(..) function returns a tuple, where the first value is the location of the image and the second the filter you used.

    def getImage(self):
    
        filter = "Images (*.png *.jpg)"
        image_obj, _ = QFileDialog.getOpenFileName(self, 'Open image', 'Desktop', filter)
        self.orig_lbl.setPixmap(QPixmap(image_obj))