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?
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?
From inside your QDialog/QWidget class, you should be able to do:
file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
None
instead of self
and it will work, like this: folder = str(QFileDialog.getExistingDirectory(None, "Select Directory"))
–
Exchange Just as simple as that:
folderpath = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select Folder')
Here, self
represents the parent window usually the QMainWindow
object.
filepath = QtWidgets.QFileDialog.getOpenFileName(self, 'Hey! Select a File')
QtWidgets.QFileDialog.getOpenFileName(self, 'Hey! Select a File')
–
Mylo In PyQt6 QFileDialog.getExistingDirectory
is the same as PyQt5, but things change for QFileDialog.getOpenFileName
which returns a tuple instead:
from PyQt6.QtWidgets import QFileDialog
file_path, filter_ = QFileDialog.getOpenFileName(self, 'Pick a file')
© 2022 - 2024 — McMap. All rights reserved.