Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to have a directory dialog

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?

like image 387
Moayyad Yaghi Avatar asked Nov 26 '10 14:11

Moayyad Yaghi


People also ask

How do I select a directory in Python?

To find the current working directory in Python, use os. getcwd() , and to change the current working directory, use os. chdir(path) .


Video Answer


2 Answers

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

file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
like image 106
TZHX Avatar answered Oct 18 '22 02:10

TZHX


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')
like image 16
Ali Sajjad Avatar answered Oct 18 '22 01:10

Ali Sajjad