Search code examples
pythonpython-3.xpyqtpyqt5openfiledialog

GUI File Picker


I am trying to use PYQT5 file picker, but there is something I am missing. I'm having two problems. the first is when the file dialog box opens and I choose a file. the whole program crashed and I get AttributeError: 'list' object has no attribute 'seek' and QWaitCondition: Destroyed while threads are still waiting that show in the terminal. The second is when I hit cancel on the file dialog the whole program crashes and it says nboundLocalError: local variable 'newdata' referenced before assignment and QWaitCondition: Destroyed while threads are still waiting. What I would like is to be able to have the dialog pop up and choose the file and then and then have the contents of that file be loaded into that variable. I'm not sure what is going wrong. I have posted my current code below. Any advice or help is greatly appreciated.

def open(self):
    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    try:
        fileToOpen = QFileDialog.getOpenFileNames(self,"Open File", "","All Files (*);;Python Files (*.py)", options=options)
    except:
        pass

    pdb.set_trace()
    if fileToOpen:
        with ZipFile(fileToOpen, 'r') as myzip:
            json_data_read = myzip.read('digest.json')
            newdata = json.loads(json_data_read)

    Functions.Loads = newdata[1]

Solution

  • getOpenFilename returns a tuple. You want the second return value so call it like

    fileToOpen, _ = getOpenFilename(...)
    

    It's because pyqt5 calls getOpenFilenameAndFilter: http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html#qfiledialog

    The second problem is because you don't init newData if there is no filename.