5

This is my first time attempting to create a GUI. I have created a simple program with a button that is supposed to open the file browser and return that file. I know PyQt5 has some built in file dialog commands, but they are not working for me. I attempted to use the tutorial found on https://pythonspot.com/en/pyqt5-file-dialog/. I also tried several other approaches from online. I cannot think of a reason why it is not working.

import sys, os
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1082, 800)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(Form)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.horizontalLayout_2.addLayout(self.horizontalLayout)

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

        self.pushButton.clicked.connect(self.openFile)


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


    # FUNCTION BELOW NOT WORKING
    def openFile(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__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

3 Answers 3

12

getOpenFileName requires as a parameter an object of type QWidget, in your case Ui_Form is not of that type.

  • First Solution: pass None:

fileName, _ = QFileDialog.getOpenFileName(None,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
  • Second solution: create a class that implements the Ui_Form visa, and in it the openFile slot is created.

class Widget(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.openFile)

    def openFile(self):   
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
        if fileName:
            print(fileName)
Sign up to request clarification or add additional context in comments.

3 Comments

Works perfectly.
I also found another solution (which I posted below). What is the difference in functionality between the two solutions?
@J.Doe what happened?
1

You need to pass a QWidget as the parent. I modified your code to save the Form and use that later.

class Ui_Form(object):
    def setupUi(self, Form):
        self.Form = Form          # <-----
        ...

    def openFile(self):   
        fileName, _ = QFileDialog.getOpenFileName(self.Form,    # <-----
        ...

Comments

1

Another solution I found was to change the function to:

def openFileNameDialog(self):    
    fname = QFileDialog.getOpenFileName()
    self.ui.lineEdit.setText(fname)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.