Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to open *.jpg or *.png files?



  • When I try to open a file from interface, I had this error. How can I fix it ?

    QWidget: Must construct a QApplication before a QWidget

    And I had this TypeError

    TypeError: getOpenFileName(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0): argument 1 has unexpected type 'Ui_MainWindow'
    My code:

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QFileDialog
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.setEnabled(True)
            MainWindow.resize(1214, 600)
            MainWindow.setStyleSheet("background-color: rgb(211, 215, 207);")
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
    
    
    
            
            self.label_source = QtWidgets.QLabel(self.centralwidget)
            self.label_source.setGeometry(QtCore.QRect(0, 160, 550, 400))
            self.label_source.setText("")
            self.label_source.setObjectName("label_source")
            
            
            
            
            
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
            self.actionOpen_Source = QtWidgets.QAction(MainWindow)
            icon = QtGui.QIcon.fromTheme("folder-open")
            self.actionOpen_Source.setIcon(icon)
            font = QtGui.QFont()
            self.actionOpen_Source.setFont(font)
            self.actionOpen_Source.setShortcutContext(QtCore.Qt.WindowShortcut)
            self.actionOpen_Source.setAutoRepeat(True)
            self.actionOpen_Source.setVisible(True)
            self.actionOpen_Source.setMenuRole(QtWidgets.QAction.TextHeuristicRole)
            self.actionOpen_Source.setObjectName("actionOpen_Source")
            self.actionOpen_Source.triggered.connect(self.openSource)
            
            
    
        
           
            
        **def openSource(self):
            openFile=QFileDialog.getOpenFileName(self,'Open Source', '../images', 'Image Files (*.jpg,*.png)')
            fname = open(openFile)
            self.label_source.setPixmap(QtGui.QPixmap(fname))**
    
            
    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_())
    


  • @elypheldia said in How to open *.jpg or *.png files?:

    argument 1 has unexpected type 'Ui_MainWindow'

    Your class Ui_MainWindow(object) means Ui_MainWindow does not derive from QWidget. QFileDialog.getOpenFileName(self will therefore fail. You probably want to pass MainWindow instead of self.

    QWidget: Must construct a QApplication before a QWidget: fix first above, maybe goes away? Else determine where it comes from (e.g. run under debugger).


Log in to reply