gpt4 book ai didi

python - 如何使用 KFileDialog 选择多个目录?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:56:05 26 4
gpt4 key购买 nike

使用PyKDE4.kio 选择多个文件我可以使用KFileDialog.getOpenFileNames(而不是KFileDialog.getOpenFileName)。如果要选择多个目录怎么办?只有 KFileDialog.getExistingDirectory

使用 KFileDialog.getOpenFileNames(filter = 'inode/directory') 并选择多个文件夹显示错误:

More than one folder has been selected and this dialog does not accept folders, so it is not possible to decide which one to enter. Please select only one folder to list it.

最佳答案

我从 JohannesMunk on qtcentre.org 找到了解决方案并将其翻译成 python

import sys
from PyQt5.QtWidgets import (QFileDialog, QAbstractItemView, QListView,
QTreeView, QApplication, QDialog)

class getExistingDirectories(QFileDialog):
def __init__(self, *args):
super(getExistingDirectories, self).__init__(*args)
self.setOption(self.DontUseNativeDialog, True)
self.setFileMode(self.Directory)
self.setOption(self.ShowDirsOnly, True)
self.findChildren(QListView)[0].setSelectionMode(QAbstractItemView.ExtendedSelection)
self.findChildren(QTreeView)[0].setSelectionMode(QAbstractItemView.ExtendedSelection)

qapp = QApplication(sys.argv)
dlg = getExistingDirectories()
if dlg.exec_() == QDialog.Accepted:
print(dlg.selectedFiles())

关于python - 如何使用 KFileDialog 选择多个目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18707261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com