0

Using a piece of example code from tutorialspoint, I am unable to open a file dialog using that code.

I am using Ubuntu MATE 16.04, python version 3.7.4, PyQt5 version 5.13.1 and the following piece of code

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class filedialogdemo(QWidget):
    def __init__(self, parent=None):
        super(filedialogdemo, self).__init__(parent)

        layout = QVBoxLayout()
        self.btn = QPushButton("QFileDialog static method demo")
        self.btn.clicked.connect(self.getfile)

        layout.addWidget(self.btn)
        self.le = QLabel("Hello")

        layout.addWidget(self.le)

        self.contents = QTextEdit()
        layout.addWidget(self.contents)
        self.setLayout(layout)
        self.setWindowTitle("File Dialog demo")

    def getfile(self):
        fname, _ = QFileDialog.getOpenFileName(self, 'Open file',
                                            '', "Image files (*.jpg *.gif)")
        print('The file name is...', fname)
        self.le.setPixmap(QPixmap(fname))

def main():
    app = QApplication(sys.argv)
    ex = filedialogdemo()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Yesterday I executed the code on a Windows computer without any trouble. And since I am not getting any error messages, just a None in return.. I cant really debug it.. at least I dont know how.. Any help is very much appreciated

To clarify what I get, here is a screen shot: enter image description here

In words: my GUI is created and I can interact with it. Which means that when I press a button, the connected function is called (since it prints out what I ask). But no file dialog/explorer is opened, hence the return of the procedure is None. Thus nothing is printed after The file name is...

EDIT2: I reinstalled the PyQt5 package with pip3, to test if that made any difference. But sadly, no..

EDIT3: As @ekhumuro rightfully said, if you run this code in a simple console (e.g. not Pycharm), then it should it run fine. And it did.

zwep
  • 1,207
  • 12
  • 26
  • Explain yourself better, from what I understand you, the popup does not open, but if the initial window is displayed, am I correct? – eyllanesc Sep 26 '19 at 17:59
  • A quick Google search gets this syntax `fname = QFileDialog.getOpenFileName(self, 'Open file', 'c:/',"Image files (*.jpg *.gif)")` and there is no ", _" not sure why you have that in there? You also did not give a source directory? – Dennis Jensen Sep 26 '19 at 18:08
  • @eyllanesc Yes that is correct. I see my GUI, so that works. Other buttons that I create work as well, but clicking on the `get_file` button does actually nothing. I will add my console output from this run, that might help – zwep Sep 26 '19 at 19:08
  • 2
    @DennisJensen The addition of `fname, _` is needed because (I believe) in PyQt5 the output of `getOpenFileName` is a tuple instead of a single string as in PyQt4. The reason I am not supplying a source directory is because then it can default to a previous one, or to home. – zwep Sep 26 '19 at 19:11

1 Answers1

2

After some more searching I came across the answer. This helped me, to add the follow option to the openFileDialog

fname, _ = QFileDialog.getOpenFileName(self, 'Open file',
                                             '', '',
                                             options=QFileDialog.DontUseNativeDialog)

This answer was found by @wagnerpeer on the following SO question

No files visible in the QFileDialog window

EDIT: @ekhumoro his comment below mine is very important and correct. This solution might only be needed when working from within an IDE

zwep
  • 1,207
  • 12
  • 26
  • 2
    This really shouldn't be necessary. I suspect that if you run the example from a stardard console (i.e. not via an IDE), the problem might go away. – ekhumoro Sep 26 '19 at 20:09