python-3.xfilepyqtfile-browser

So I'm trying to make an app using PyQt5. Tried to give some functionality to a button, throws an error


def openFileNameDialog(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
        if fileName:
            print(fileName)

For this code, this is my output ,

fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files ();;Python Files (.py)", options=options) 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'

How do I fix this ?

(Post Edit) : This is my GUI code :

def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(443, 427)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(350, 100, 75, 23))
        self.pushButton.setObjectName("pushButton")
        **self.pushButton.clicked.connect(self.openFileNameDialog)**
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(40, 100, 281, 21))
        self.lineEdit.setObjectName("lineEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 443, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Browse"))

The error is thrown when I run the python script.


Solution

  • Thanks for providing the GUI code. I believe the error most likely arises from inheritance complications. I am not sure why you are passing a separate MainWindow object to various functions. Instead, try to inherit from QMainWindow() and assign all the UI elements to self.

    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    
    class MainWindow(QMainWindow):
    
        def __init__(self):
            super().__init__()
            self.setObjectName("MainWindow")
            self.resize(443, 427)
            self.centralwidget = QWidget(self)
            self.centralwidget.setObjectName("centralwidget")
            self.pushButton = QPushButton(self.centralwidget)
            self.pushButton.setGeometry(QRect(350, 100, 75, 23))
            self.pushButton.setObjectName("pushButton")
            self.pushButton.clicked.connect(self.openFileNameDialog)
            self.lineEdit = QLineEdit(self.centralwidget)
            self.lineEdit.setGeometry(QRect(40, 100, 281, 21))
            self.lineEdit.setObjectName("lineEdit")
            self.setCentralWidget(self.centralwidget)
            self.menubar = QMenuBar(self)
            self.menubar.setGeometry(QRect(0, 0, 443, 21))
            self.menubar.setObjectName("menubar")
            self.setMenuBar(self.menubar)
            self.statusbar = QStatusBar(self)
            self.statusbar.setObjectName("statusbar")
            self.setStatusBar(self.statusbar)
    
            self.retranslateUi()
            QMetaObject.connectSlotsByName(self)
    
        def retranslateUi(self):
            _translate = QCoreApplication.translate
            self.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.pushButton.setText(_translate("MainWindow", "Browse"))
    
        def openFileNameDialog(self):
            options = QFileDialog.Options()
            options |= QFileDialog.DontUseNativeDialog
            fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
            if fileName:
                print(fileName)
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        MW = MainWindow()
        MW.show()
        sys.exit(app.exec_())