如何从文件对话框中选择多个文件并同时打开并访问它们

How to choose multiple files from file dialog and open at the same time and access them

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog
from PyQt5 import QtCore
import sys
 
 
def dialog():
    file , check = QFileDialog.getOpenFileName(None, "QFileDialog.getOpenFileName()",
                    "", "All Files (*);;Python Files (*.py);;Text Files (*.txt)")
    if check:
        print(file)
 
app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(400,400,300,300)
win.setWindowTitle("CodersLegacy")
  
button = QPushButton(win)
button.setText("Press")
button.clicked.connect(dialog)
button.move(50,50)
 
win.show()
sys.exit(app.exec_())

这是 select 的代码,当我按下按钮时打开单个文件。但是,这个怎么改成select多个文件同时打开。我尝试了语法,不幸的是,我找不到。

你必须使用 QFileDialog::getOpenFileNames() 方法,还有元组的第二个值 returns 不是检查而是指示使用的过滤器的字符串,如果你想验证那么你必须使用文件名的大小:

filenames, _ = QFileDialog.getOpenFileNames(
    None,
    "QFileDialog.getOpenFileNames()",
    "",
    "All Files (*);;Python Files (*.py);;Text Files (*.txt)",
)
if filenames:
    for filename in filenames:
        print(filename)