ダイアログでファイルを選択する

2016/09/03

Python2.7.10, PySide1.2.2

dir = self.sourceDir
filters = "Text files (*.txt);;Images (*.png *.xpm *.jpg)"
selected_filter = "Images (*.png *.xpm *.jpg)"
options = 0 
fileObj = QFileDialog.getOpenFileName(self, " File dialog ", dir, filters, selected_filter, options)

optionsはよーわからん。


クリックでファイルダイアログを開く

# -*- coding: utf-8 -*-

import sys,os
from PySide import QtGui

class MyMainWindow(QtGui.QMainWindow):
    def __init__(self, *argv, **keywords ):
        super(MyMainWindow,self).__init__(*argv,**keywords)

    def mousePressEvent(self,event):
        parent = self
        dir_path = dp = os.path.expanduser('~') + '/Desktop'
        caption = u'画像を開く'
        filters = "Text files (*.txt);;Images (*.png *.xpm *.jpg)"
        selected_filter = "Images (*.png *.xpm *.jpg)"
        options = 0
        fileObj = QtGui.QFileDialog.getOpenFileName(parent,caption,
                                     dir_path,filters,selected_filter,options)
        filepath = fileObj[0]
        print filepath

def main():
    app = QtGui.QApplication(sys.argv)
    w = MyMainWindow()
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()


開くファイルの種類

AnyFile = getSaveFileName
ExistingFile = getOpenFileName
Directory = getExistingDirectory
ExistingFiles = getOpenFileNames


How to use options in QFileDialog.getOpenFileName?
http://stackoverflow.com/questions/27998880/how-to-use-options-in-qfiledialog-getopenfilename