Python 3.x 选择要在PyQt5中创建目录的目录
我正在创建另存为。。。PyQt5上的函数 函数应该打开一个文件对话框,让用户指定他们想要的目录 以蒸汽等特定应用为例。当您保存steam时,它们允许您选择一个目录来保存它 用户输入:D:// 但随后他们将为用户创建D://Steam/。此D://Steam/文件夹的名称是默认名称,可以更改为用户想要的任何内容,例如:D://asdfgh/并且所有内容都将在那里下载 您可以将该函数与另存为相同。。。函数,但它不是Word文档,而是一个目录 这是我当前的代码Python 3.x 选择要在PyQt5中创建目录的目录,python-3.x,pyqt,dialog,pyqt5,Python 3.x,Pyqt,Dialog,Pyqt5,我正在创建另存为。。。PyQt5上的函数 函数应该打开一个文件对话框,让用户指定他们想要的目录 以蒸汽等特定应用为例。当您保存steam时,它们允许您选择一个目录来保存它 用户输入:D:// 但随后他们将为用户创建D://Steam/。此D://Steam/文件夹的名称是默认名称,可以更改为用户想要的任何内容,例如:D://asdfgh/并且所有内容都将在那里下载 您可以将该函数与另存为相同。。。函数,但它不是Word文档,而是一个目录 这是我当前的代码 saveLocation = QFile
saveLocation = QFileDialog.getExistingDirectory(None, "Save As...", os.getenv('HOME'))
if saveLocation:
currentSaveLocation = saveLocation
fromDirectory = Main.tempPath
toDirectory = saveLocation
copy_tree(fromDirectory, toDirectory)
我无法将文件保存到命名目录中。为了获得可能尚未存在的路径,使用QFileDialog的静态方法不是可行的解决方案。此外,我们不能使用本机OS文件对话框,因为与静态方法一样,它们没有提供足够的控制 考虑到以下几个方面,我们需要做一些小的“黑客攻击”:
- 如果对话框设置为
文件模式,则写入不存在的路径将禁用打开按钮李>目录
- 即使该按钮被禁用,当在带有不存在路径的行编辑中按Return键时,对话框仍会抱怨不存在路径李>
- 使用非本机文件对话框,这样我们就可以访问子部件李>
- 获取“打开”按钮,以便我们可以在需要时手动启用它李>
- 获取对话框的行编辑,并在路径文本发生更改且检测到有效路径时手动启用按钮李>
- 覆盖对话框的
方法以忽略警告,并使用基本accept()
方法李>QDialog.accept()
你的问题不是很清楚。使用
getExistingDirectory
可以获得现有路径,是否希望用户能够选择“目标”目录,无论它是否存在?@musicamante是,我希望用户能够选择目标目录,无论它是否存在。目录深度应仅为当前目录的+1,例如C://newdir是可接受的,但C://new/dir是不可接受的(假设C驱动器中不存在new)
class Test(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QHBoxLayout(self)
self.pathEdit = QtWidgets.QLineEdit(placeholderText='Select path...')
self.button = QtWidgets.QToolButton(text='...')
layout.addWidget(self.pathEdit)
layout.addWidget(self.button)
self.button.clicked.connect(self.selectTarget)
def selectTarget(self):
dialog = QtWidgets.QFileDialog(self)
if self.pathEdit.text():
dialog.setDirectory(self.pathEdit.text())
dialog.setFileMode(dialog.Directory)
# we cannot use the native dialog, because we need control over the UI
options = dialog.Options(dialog.DontUseNativeDialog | dialog.ShowDirsOnly)
dialog.setOptions(options)
def checkLineEdit(path):
if not path:
return
if path.endswith(QtCore.QDir.separator()):
return checkLineEdit(path.rstrip(QtCore.QDir.separator()))
path = QtCore.QFileInfo(path)
if path.exists() or QtCore.QFileInfo(path.absolutePath()).exists():
button.setEnabled(True)
return True
# get the "Open" button in the dialog
button = dialog.findChild(QtWidgets.QDialogButtonBox).button(
QtWidgets.QDialogButtonBox.Open)
# get the line edit used for the path
lineEdit = dialog.findChild(QtWidgets.QLineEdit)
lineEdit.textChanged.connect(checkLineEdit)
# override the existing accept() method, otherwise selectedFiles() will
# complain about selecting a non existing path
def accept():
if checkLineEdit(lineEdit.text()):
# if the path is acceptable, call the base accept() implementation
QtWidgets.QDialog.accept(dialog)
dialog.accept = accept
if dialog.exec_() and dialog.selectedFiles():
path = QtCore.QFileInfo(dialog.selectedFiles()[0]).absoluteFilePath()
self.pathEdit.setText(path)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Test()
w.show()
sys.exit(app.exec_())