PyQt: How can I get a large list of filenames from the user?
-
15-03-2021 - |
Question
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_())
OTHER TIPS
I would suggest you to let the user use drag & drop to add files directly from their favorite file browser. As I did this in wxpython without any trouble and user-feedbacks are pretty good :)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow