简体   繁体   English

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

[英]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.

相关问题 用tkinter选择多个文本文件后,如何同时打开和操作它们? - How to open and manipulate multiple text files all at the same time after selecting them with tkinter? 如何从具有 python 文件列表的单个文件中打开多个文件以及如何对它们进行处理? - how to open multiple file from single file which is having list of files in python and how to do processing on them? 如何在wxPython的打开文件对话框中选择没有任何扩展名的文件? - How to choose a file without any extension in an open file dialog in wxPython? 如何允许Python从2个文本文件(同一行)中选择一条随机行,然后将其存储为变量? - How do I allow Python to choose a random line from 2 text files (that are the same line) and then store them as variables? 如何在 Python 中同时打开多个文件? - How can I open multiple files at the same time in Python? 如何从 zip 文件的多个文件夹访问多个 CSV 文件 - How to access multiple CSV files that share the same name from multiple folders from a zip file 从一个文件中打开多个文件 - Open multiple files from a file 如何使用xlsx中的相同列在Python中打开多个xslx文件 - How to open multiple xslx files in Python with the same columns from the xlsx Azure function - 多个文件同时进入存储 blob:如何按顺序处理它们 - Azure function - multiple files coming in storage blob at the same time: how to make them be processed sequentially 如何在python中导入多个csv股票文件并同时统计? - How to import multiple csv stocks files in python and do statistics on them at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM