【问题标题】:AttributeError: 'QDialog' object has no attribute 'QFileDialog' [duplicate]AttributeError:“QDialog”对象没有属性“QFileDialog”[重复]
【发布时间】:2020-04-02 08:40:45
【问题描述】:

我又来了,我早上早些时候运行了这个脚本,它工作了。重新启动笔记本电脑后,我开始收到以下错误

AttributeError: 'QDialog' 对象没有属性 'QFileDialog'。

任何建议。

谢谢

def pushButton_handler(self):
        print("Button pressed")
        #self.open_dialog_box()


    def pushButton_handler(self):
        #fileName = Dialog.QFileDialog.getOpenFileName(self, "Open File", "", "CSV Files (*.csv)");
        fnames = Dialog.QFileDialog.getOpenFileNames(self, "Open Data File", "", "CSV data files (*.csv)")

        self.pathLE.setText(fileName)
        df = pd.read_csv(fileName)
        print(df)




---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-fa4549e61e23> in pushButton_handler(self)
    288     def pushButton_handler(self):
    289         #fileName = Dialog.QFileDialog.getOpenFileName(self, "Open File", "", "CSV Files (*.csv)");
--> 290         fnames = Dialog.QFileDialog.getOpenFileNames(self, "Open Data File", "", "CSV data files (*.csv)")
    291 
    292         self.pathLE.setText(fileName)

AttributeError: 'QDialog' object has no attribute 'QFileDialog'

【问题讨论】:

  • 尝试使用QtWidgets.QFileDialog(...)
  • 需要查看“Dialog”实例的类类型。直接尝试 fnames= QFileDialog.getExistingDirectory(self, "Open Data File", "", "CSV data files (*.csv)") 其中 self 是对话框的父级。它可以是 self.someFormComponent。 QFileDialog 不会从具有确定继承的特定组件中调用,您只需将父级作为参数添加到 getOpenFileNames 函数中。
  • 我试过 QtWidgets.QFileDialog(...) 并出现以下错误: TypeError: getOpenFileName(parent: QWidget = None, caption: str = '', directory: str = '',过滤器:str = '',initialFilter:str = '',选项:Union[QFileDialog.Options, QFileDialog.Option] = 0):参数 1 具有意外类型 'Ui_Dialog'
  • RustyBucketBay。你的意思是我需要取类的名称,即我的情况是 UI_Dialog。下面的代码类 class Ui_Dialog(object): def setupUi(self, Dialog):
  • 像任何QWidget 一样,QFileDialog 对象的父对象应该是QWidget 的一个实例。在这种情况下,它可能应该是您用作setupUi 的输入参数的Dialog 的实例。

标签: python pyqt5


【解决方案1】:

查找 QFileDialog.getOpenFileName 的工作示例,以防它对您有所帮助。

from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
                             QAction, QFileDialog, QApplication)
from PyQt5.QtGui import QIcon
import sys


class Example(QMainWindow):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.textEdit = QTextEdit()
        self.setCentralWidget(self.textEdit)
        self.statusBar()

        openFile = QAction(QIcon('open.png'), 'Open', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip('Open new File')
        openFile.triggered.connect(self.showDialog)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(openFile)

        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('File dialog')
        self.show()

    def showDialog(self):
        fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')

        if fname[0]:
            f = open(fname[0], 'r')

            with f:
                data = f.read()
                self.textEdit.setText(data)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-26
    • 2019-11-22
    • 2019-03-18
    • 2019-03-10
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多