文件对话框不能与PyQt5一起使用?

时间:2017-05-19 17:19:58

标签: python pyqt pyqt5 qt-designer qfiledialog

这是我第一次尝试创建GUI。我创建了一个带有按钮的简单程序,该按钮应该打开文件浏览器并返回该文件。我知道PyQt5有一些内置的文件对话框命令,但它们对我不起作用。我试图使用https://pythonspot.com/en/pyqt5-file-dialog/上的教程。我还尝试了其他几种在线方法。我想不出它为什么不起作用的原因。

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 个答案:

答案 0 :(得分:6)

getOpenFileName需要一个类型为QWidget的对象作为参数,在您的情况下,Ui_Form不属于该类型。

  • 第一个解决方案:传递None
fileName, _ = QFileDialog.getOpenFileName(None,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
  • 第二个解决方案:创建一个实现Ui_Form签证的类,并在其中创建openFile插槽。
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)

答案 1 :(得分:1)

您需要将QWidget作为父级传递。我修改了您的代码以保存Form并稍后再使用。

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

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

答案 2 :(得分:0)

我发现的另一个解决方案是将功能更改为:

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