QFileDialog.getExistingDirectory
时间: 2024-05-08 15:15:46 浏览: 223
QFileDialog.getExistingDirectory is a method in PyQt5 that opens a dialog to allow the user to select an existing directory. It returns the path of the selected directory as a string.
The syntax for this method is:
```python
QFileDialog.getExistingDirectory(parent, caption, directory, options)
```
where:
- `parent`: the parent widget of the dialog (optional)
- `caption`: the caption of the dialog (optional)
- `directory`: the starting directory for the file dialog (optional)
- `options`: the options for the file dialog (optional)
The `options` argument is a combination of the following flags:
- `QFileDialog.ShowDirsOnly`: only allows the selection of directories
- `QFileDialog.DontResolveSymlinks`: does not resolve symbolic links
- `QFileDialog.DontUseNativeDialog`: uses the Qt dialog instead of the native dialog
Here's an example of how to use this method to get the path of an existing directory:
```python
from PyQt5.QtWidgets import QApplication, QFileDialog
app = QApplication([])
directory = QFileDialog.getExistingDirectory(None, "Select Directory")
print(directory)
```
This will open the file dialog and allow the user to select a directory. Once a directory is selected, the path will be printed to the console.
阅读全文