Python PyQt - QFileDialog - 直接浏览到一个文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38746002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PyQt - QFileDialog - directly browse to a folder?
提问by YaronGh
Is there any way to directly browse to a folder using QFileDialog?
有没有办法使用 QFileDialog 直接浏览到文件夹?
Meaning, instead of double clicking on each folder while navigating to the destination folder, simply enter the path somewhere or use a hotkey like the one (Shift+Command+G) in Finder on Mac OS X.
这意味着,无需在导航到目标文件夹时双击每个文件夹,只需在某处输入路径或使用 Mac OS X 上 Finder 中的热键(Shift+Command+G)即可。
Thanks!
谢谢!
EDIT:(my code)
编辑:(我的代码)
filter = "Wav File (*.wav)"
self._audio_file = QtGui.QFileDialog.getOpenFileName(self, "Audio File",
"/myfolder/folder", filter)
self._audio_file = str(self._audio_file)
回答by ekhumoro
If you use the static QFileDialog
functions, you'll get a nativefile-dialog, and so you'll be limited to the functionality provided by the platform. You can consult the documentation for your platform to see if the functionality you want is available.
如果您使用静态QFileDialog
函数,您将获得一个本机文件对话框,因此您将受限于平台提供的功能。您可以查阅您平台的文档以查看您想要的功能是否可用。
If it's not available, you'll have to settle for Qt's built-infile-dialog, and add your own features. For your specific use-case, this should be easy, because the built-in dialog already seems to have what you want. It has a side-barthat shows a list of "Places" that the user can navigate to directly. You can set your own places like this:
如果它不可用,您将不得不接受 Qt 的内置文件对话框,并添加您自己的功能。对于您的特定用例,这应该很容易,因为内置对话框似乎已经有了您想要的东西。它有一个侧栏,显示用户可以直接导航到的“地点”列表。您可以像这样设置自己的位置:
dialog = QtGui.QFileDialog(self, 'Audio Files', directory, filter)
dialog.setFileMode(QtGui.QFileDialog.DirectoryOnly)
dialog.setSidebarUrls([QtCore.QUrl.fromLocalFile(place)])
if dialog.exec_() == QtGui.QDialog.Accepted:
self._audio_file = dialog.selectedFiles()[0]
回答by Derek R.
Here's a convenience function for quickly making an open/save QFileDialog
.
这是一个快速打开/保存的便利功能QFileDialog
。
from PyQt5.QtWidgets import QFileDialog, QDialog
from definitions import ROOT_DIR
from PyQt5 import QtCore
def FileDialog(directory='', forOpen=True, fmt='', isFolder=False):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
options |= QFileDialog.DontUseCustomDirectoryIcons
dialog = QFileDialog()
dialog.setOptions(options)
dialog.setFilter(dialog.filter() | QtCore.QDir.Hidden)
# ARE WE TALKING ABOUT FILES OR FOLDERS
if isFolder:
dialog.setFileMode(QFileDialog.DirectoryOnly)
else:
dialog.setFileMode(QFileDialog.AnyFile)
# OPENING OR SAVING
dialog.setAcceptMode(QFileDialog.AcceptOpen) if forOpen else dialog.setAcceptMode(QFileDialog.AcceptSave)
# SET FORMAT, IF SPECIFIED
if fmt != '' and isFolder is False:
dialog.setDefaultSuffix(fmt)
dialog.setNameFilters([f'{fmt} (*.{fmt})'])
# SET THE STARTING DIRECTORY
if directory != '':
dialog.setDirectory(str(directory))
else:
dialog.setDirectory(str(ROOT_DIR))
if dialog.exec_() == QDialog.Accepted:
path = dialog.selectedFiles()[0] # returns a list
return path
else:
return ''
回答by ospahiu
In PyQt 4
, you're able to just add a QFileDialog
to construct a window that has a path textfield embedded inside of the dialog. You can paste your path in here.
在 中PyQt 4
,您可以只添加一个QFileDialog
来构造一个窗口,该窗口在对话框内嵌入了一个路径文本字段。您可以在此处粘贴您的路径。
QtGui.QFileDialog.getOpenFileName(self, 'Select file') # For file.
For selecting a directory:
选择目录:
QtGui.QFileDialog.getExistingDirectory(self, 'Select directory')
Each will feature a path textfield:
每个都有一个路径文本字段:
回答by user3160702
Use getExistingDirectory
method instead:
改用getExistingDirectory
方法:
from PyQt5.QtWidgets import QFileDialog
dialog = QFileDialog()
foo_dir = dialog.getExistingDirectory(self, 'Select an awesome directory')
print(foo_dir)
回答by BPL
Below you'll find a simple test which opens directly the dialog at a certain path, in this case will be the current working directory. If you want to open directly another path you can just use python's directory functions included in os.path module:
下面你会发现一个简单的测试,它直接在特定路径打开对话框,在这种情况下将是当前工作目录。如果你想直接打开另一个路径,你可以使用 os.path 模块中包含的 python 目录函数:
import sys
import os
from PyQt4 import QtGui
def test():
filename = QtGui.QFileDialog.getOpenFileName(
None, 'Test Dialog', os.getcwd(), 'All Files(*.*)')
def main():
app = QtGui.QApplication(sys.argv)
test()
sys.exit(app.exec_())
if __name__ == "__main__":
main()