如何拥有目录对话框

问题描述 投票:0回答:3

在 PyQt 中,如何显示一个仅显示和选择目录(而不是文件)的文件浏览器?

如何检索所选目录的名称?

python pyqt pyqt4 file-browser
3个回答
120
投票

在 QDialog/QWidget 类中,您应该能够执行以下操作:

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

22
投票

就这么简单:

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

这里,

self
代表父窗口,通常是
QMainWindow
对象。

文件对话框类似:

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

0
投票

在 PyQt6 中

QFileDialog.getExistingDirectory
与 PyQt5 相同,但
QFileDialog.getOpenFileName
的情况发生了变化,它返回一个元组:

from PyQt6.QtWidgets import QFileDialog
file_path, filter_ = QFileDialog.getOpenFileName(self, 'Pick a file')
© www.soinside.com 2019 - 2024. All rights reserved.