Skip to main content
 首页 » 编程设计

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

2025年05月04日17jackei

pyqt 中的 FileDialog 是从用户那里获取文件的一个路径的好方法,但是有没有一种从用户那里获取大量文件选择的好方法?

请您参考如下方法:

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