2013-03-26 99 views
1

我正在使用Qt.Gui.QFileDialog.getOpenFileName方法打開文件。如果我將默認文件目錄設置爲"/Users/me/Documents/data/2013/today/",則對話框打開爲"/Users/me/Documents/data/2013/"並選擇"today"文件夾。如何獲得在窗口中打開的正確的默認文件夾?PyQt getOpenFileName默認目錄

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/

回答

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_()) 
+0

我不想返回目錄。如果需要,可以解析返回的字符串以獲取目錄。我想要OpenFileDialog窗口在正確的目錄中打開。我編輯了這個問題來澄清。 – JMD

+0

@JMD我沒有得到我的Debian操作系統中的這種行爲,請仔細檢查您的問題並查看我的[更新後的答案](http://stackoverflow.com/a/15634376/1006989) – 2013-03-27 09:35:25