PyQt: 如何从用户那里获取大量文件名?

1 投票
2 回答
5651 浏览
提问于 2025-04-17 08:20

在pyqt中,文件对话框是一种很好的方式,可以让用户选择一个文件的路径。但是,如果想让用户选择很多个文件,有没有好的方法呢?

2 个回答

0

我建议你让用户可以通过拖放的方式,直接从他们喜欢的文件浏览器中添加文件。因为我在使用wxpython的时候这样做没有遇到任何问题,而且用户反馈也很好哦 :)

6

使用 QFileDialog.getOpenFileNames 可以让用户选择多个文件:

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

撰写回答