Reputation: 338
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_())
Upvotes: 4
Views: 15197
Reputation: 243955
getOpenFileName
requires as a parameter an object of type QWidget
, in your case Ui_Form is not of that type.
None
:fileName, _ = QFileDialog.getOpenFileName(None,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
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)
Upvotes: 11
Reputation: 338
Another solution I found was to change the function to:
def openFileNameDialog(self):
fname = QFileDialog.getOpenFileName()
self.ui.lineEdit.setText(fname)
Upvotes: 1
Reputation: 5821
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, # <-----
...
Upvotes: 1