Search code examples
pythonpyqtpyqt4qfiledialog

PyQt: How can I get a large list of filenames from the user?


The FileDialog in pyqt is an excellent way to get one path for a file from the user, but is there a good way to get a large number of file selections from the user?


Solution

  • Use QFileDialog.getOpenFileNames to allow the user to select multiple files:

    from PyQt4 import QtGui, QtCore
    
    class Window(QtGui.QWidget):
        def __init__(self):
            QtGui.QWidget.__init__(self)
            layout = QtGui.QVBoxLayout(self)
            self.button = QtGui.QPushButton('Select Files', self)
            layout.addWidget(self.button)
            self.button.clicked.connect(self.handleButton)
    
        def handleButton(self):
            title = self.button.text()
            for path in QtGui.QFileDialog.getOpenFileNames(self, title):
                print path
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication(sys.argv)
        window = Window()
        window.show()
        sys.exit(app.exec_())