Найти - Пользователи
Полная версия: Как выбрать несколько файлов в PyQt5
Начало » Python для новичков » Как выбрать несколько файлов в PyQt5
1
AxaRu
Коллеги, пытаюсь заставить PyQt сделать выбор нескольких файлов. Ничего не получается.

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
    QAction, QFileDialog, QApplication)
from PyQt5.QtGui import QIcon
class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.textEdit = QTextEdit()
        self.setCentralWidget(self.textEdit)
        self.statusBar()
        openFile = QAction(QIcon('open.png'), 'Open', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip('Open new File')
        openFile.triggered.connect(self.showDialog)
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(openFile)
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('File dialog')
        self.show()
    def showDialog(self):
        data = ''                            #  здесь я храню список файлов
        dialogSelectFiles = QFileDialog()
        # следующие две строки не включают множественный выбор
        dialogSelectFiles.setFileMode(QFileDialog.ExistingFiles)
        dialogSelectFiles.setOption(QFileDialog.DontUseNativeDialog)
        # здесь я хочу прочитать список файлов и сохранить его в data
        for fname in dialogSelectFiles.getOpenFileName(self, 'Open file', '/home/axa/Stuff'):
            data = data + fname
        # В текстовом редакторе показать список
        self.textEdit.setText(data)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Подскажите, кто знает.
AxaRu
Вот так всегда. Задашь вопрось, а потом сам находишь решение.

#!/usr/bin/python3
# -*- coding: utf-8 -*-
 
import sys
from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
    QAction, QFileDialog, QApplication)
from PyQt5.QtGui import QIcon
 
class Example(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        self.initUI()
 
 
    def initUI(self):
 
        self.textEdit = QTextEdit()
        self.setCentralWidget(self.textEdit)
        self.statusBar()
 
        openFile = QAction(QIcon('open.png'), 'Open', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip('Open new File')
        openFile.triggered.connect(self.showDialog)
 
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(openFile)
 
 
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('File dialog')
        self.show()
 
 
    def showDialog(self):
 
 
        dialogSelectFiles = QFileDialog()
        dialogSelectFiles.setFileMode(QFileDialog.ExistingFiles)      # включение множественного выбора
 
        #dialogSelectFiles.setOption(QFileDialog.DontUseNativeDialog) # независимо от этой опции в любом
                                                                      # случае используется не нативный
                                                                      # диалог
 
        dialogSelectFiles.exec_()
 
        data = dialogSelectFiles.selectedFiles()
        self.textEdit.setText('\n'.join(data))
 
 
if __name__ == '__main__':
 
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
py.user.next
AxaRu
dialogSelectFiles.getOpenFileName(self, 'Open file', '/home/axa/Stuff'):
dialogSelectFiles.getOpenFileNames(self, 'Open file', '/home/axa/Stuff'):
AxaRu
py.user.next
спасибо огромное.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB