0

I am again, I have run this script earlier morning and it worked. after restarting my laptop I start to get the following error

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

Any advice.

thanks

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'
Dean
  • 199
  • 2
  • 4
  • 14
  • Try using `QtWidgets.QFileDialog(...)`. – Heike Dec 09 '19 at 09:42
  • It would be needed to see the type of class the "Dialog" instace is. Try directly fnames= QFileDialog.getExistingDirectory(self, "Open Data File", "", "CSV data files (*.csv)") where self is the parent of the dialog. It can be self.someFormComponent. The QFileDialog does not be called from a particular component with determined inheritance, you just need to add the parent as an argument in the getOpenFileNames function. – rustyBucketBay Dec 09 '19 at 09:47
  • I have tried QtWidgets.QFileDialog(...) and the following error came up: 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_Dialog' – Dean Dec 09 '19 at 09:47
  • RustyBucketBay. Do you mean i need to take the name of the class i.e im my case UI_Dialog. code below for class class Ui_Dialog(object): def setupUi(self, Dialog): – Dean Dec 09 '19 at 09:52
  • Like any `QWidget`, the parent of a `QFileDialog` object should be an instance of `QWidget`. In this case it should probably be the instance of `Dialog` that you use as the input parameter for `setupUi`. – Heike Dec 09 '19 at 10:00
  • @Dean, I mean that you can try directly: fnames = QFileDialog.getExistingDirectory(self, "Open Data File", "", "CSV data files (*.csv)"). (The same line you have removing the Dialog. from the beginning). As Heike says, self in the def pushButton_handler(self): should be a class inheriting from Qwidget. It will very probably be, but with no more code being shown, cannot say. Try this line, and I it doesn't work share the full script for better assistance – rustyBucketBay Dec 09 '19 at 10:10
  • Thanks Heike it worked but the path = self.lineEdit.text(fileName) has an error AttributeError: 'Ui_Dialog' object has no attribute 'lineEdit' any idea. – Dean Dec 09 '19 at 10:11

1 Answers1

1

Find a working sample of QFileDialog.getOpenFileName in case it might be helpful for you.

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_())
rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47