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:
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.