Форум сайта python.su
1
Коллеги, пытаюсь заставить 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 (Март 27, 2016 11:55:31)
Офлайн
1
Вот так всегда. Задашь вопрось, а потом сам находишь решение.
#!/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_())
Отредактировано AxaRu (Март 27, 2016 13:10:50)
Офлайн
857
AxaRudialogSelectFiles.getOpenFileName(self, 'Open file', '/home/axa/Stuff'):
dialogSelectFiles.getOpenFileNames(self, 'Open file', '/home/axa/Stuff'):
Офлайн
1
py.user.next
спасибо огромное.
Офлайн