[英]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_())
Here is the code to select and open a single file when I press a button.这是 select 的代码,当我按下按钮时打开一个文件。 However, how can this is changed to select multiple files and open at the same time.但是,这怎么能改成select多个文件同时打开。 I tried for the syntax, unfortunately, I could not find one.我尝试了语法,不幸的是,我找不到。
You have to use the QFileDialog::getOpenFileNames()
method, also the second value of the tuple that returns is not a check but a string that indicates the filter used, if you want to verify then you have to use the size of the filenames:您必须使用QFileDialog::getOpenFileNames()
方法,返回的元组的第二个值不是检查,而是指示使用的过滤器的字符串,如果要验证,则必须使用文件名的大小:
filenames, _ = QFileDialog.getOpenFileNames(
None,
"QFileDialog.getOpenFileNames()",
"",
"All Files (*);;Python Files (*.py);;Text Files (*.txt)",
)
if filenames:
for filename in filenames:
print(filename)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.