在pyqt5中如何打开文件夹
时间: 2023-08-11 14:09:21 浏览: 270
pyqt5实现文件批量操作
在PyQt5中,可以使用QFileDialog类来打开文件夹。具体步骤如下:
1. 导入QFileDialog类:
```python
from PyQt5.QtWidgets import QFileDialog
```
2. 创建QFileDialog对象:
```python
dialog = QFileDialog()
```
3. 设置对话框的打开模式为打开文件夹:
```python
dialog.setFileMode(QFileDialog.Directory)
```
4. 调用exec_()方法显示对话框,并获取用户选择的文件夹路径:
```python
if dialog.exec_() == QFileDialog.Accepted:
folder_path = dialog.selectedFiles()[0]
```
完整代码示例:
```python
from PyQt5.QtWidgets import QApplication, QFileDialog
app = QApplication([])
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.Directory)
if dialog.exec_() == QFileDialog.Accepted:
folder_path = dialog.selectedFiles()[0]
print('选择的文件夹路径为:', folder_path)
```
阅读全文