how to have a directory dialog

PythonPyqtPyqt4File Browser

Python Problem Overview


In PyQt, how does one display a file browser that shows and selects only directories (not files)?

And how does one retrieve the name of the selected directory?

Python Solutions


Solution 1 - Python

From inside your QDialog/QWidget class, you should be able to do:

file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))

Solution 2 - Python

Just as simple as that:

folderpath = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select Folder')

Here, self represents the parent window usually the QMainWindow object.

Similarly for File dialog:
filepath = QtWidgets.QFileDialog.getOpenFileName(self, 'Hey! Select a File')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMoayyad YaghiView Question on Stackoverflow
Solution 1 - PythonTZHXView Answer on Stackoverflow
Solution 2 - PythonAli SajjadView Answer on Stackoverflow