1

我正在使用 Qt.Gui.QFileDialog.getOpenFileName 方法打开文件。如果我将默认文件目录设置为"/Users/me/Documents/data/2013/today/"打开的对话框"/Users/me/Documents/data/2013/""today"选择文件夹。如何获得要在窗口中打开的正确默认文件夹?

fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', "/Users/me/Documents/data/2012/20121102/images/",'*.npy')

在 中打开一个对话框/Users/me/Documents/data/2012/20121102。我想打开窗户/Users/me/Documents/data/2012/20121102/images/

4

2 回答 2

0

最可能的原因是在您请求打开该文件时该文件不存在。打开文件夹使用QtGui.QFileDialog.getExistingDirectory

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.pushButtonOpen = QtGui.QPushButton(self)
        self.pushButtonOpen.setText("Open Folder")
        self.pushButtonOpen.clicked.connect(self.on_pushButtonOpen_clicked)

        self.layoutHorizontal = QtGui.QHBoxLayout(self)
        self.layoutHorizontal.addWidget(self.pushButtonOpen)

    @QtCore.pyqtSlot()
    def on_pushButtonOpen_clicked(self):
        folderName = QtGui.QFileDialog.getExistingDirectory(
            self, "Open Directory",
            "/path/to/folder",
            QtGui.QFileDialog.ShowDirsOnly | QtGui.QFileDialog.DontResolveSymlinks,
        )

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    sys.exit(app.exec_())
于 2013-03-26T10:11:55.380 回答
0

对于某人的信息(即使 8 年过去了)

您可以将directory参数传递给getOpenFileNamePyQt5 中的默认文件夹。

于 2021-07-14T05:58:30.070 回答