Извините, у меня есть PyQt5
import sys
import os
import csv
#from PySide.QtCore import *
#from PySide.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Example(QMainWindow):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.dirPath = "E:/_Qt/Python-Examples/_PyQt5/Test"
self.filePath = ""
self.btn = QPushButton('scan')
self.btn.clicked.connect(self.open_file)
self.btnSee = QPushButton('See file')
self.btnSee.clicked.connect(lambda filePath=self.filePath: self.see_file(self.filePath))
self.lay1 = QVBoxLayout()
self.lay1.addWidget(self.btn)
self.lay1.addWidget(self.btnSee)
self.scrollArea = QScrollArea()
self.scrollArea.setBackgroundRole(QPalette.Light)
self.scrollArea.setWidgetResizable(True)
self.mainLayout = QGridLayout()
self.mainLayout.addLayout(self.lay1,0,0)
self.mainLayout.addWidget(self.scrollArea,0,1)
self.setCentralWidget(QWidget(self))
self.centralWidget().setLayout(self.mainLayout)
self.setGeometry(100,100,800,500)
def open_file(self):
self.w = QWidget()
self.lay = QVBoxLayout()
self.model = QStandardItemModel(self)
self.tableView = QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
self.tableView.clicked[QModelIndex].connect(self.onClick)
#self.appendRowItems("/home/cioc/Documents/GPR/GRP")
self.appendRowItems(self.dirPath)
self.lay.addWidget(self.tableView)
self.w.setLayout(self.lay)
self.scrollArea.setWidget(self.w)
def appendRowItems(self, dir):
for root, dirs, files in os.walk(dir):
if root == dir:
for file in files:
self.model.appendRow(QStandardItem(file))
# self.model.isSelectable(True).connect(onFileClicked)
def onClick(self, ind):
self.filePath = self.dirPath +"/"+ self.model.item(ind.row(), ind.column()).text()
#def onFileClicked(self, file):
def see_file(self, file):
self.w = QWidget()
self.lay = QVBoxLayout()
self.model = QStandardItemModel(self)
self.tableView = QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
try:
#with open(file.fileName(), "rb") as fileInput:
with open(file, "r") as fileInput: # !!! "r"
for row in csv.reader(fileInput):
items = [
QStandardItem(field)
for field in row
]
#self.model.appendRow(items)
if items: self.model.appendRow(items)
except Exception as err:
QMessageBox.warning(self, 'Error', str(err))
self.lay.addWidget(self.tableView)
self.w.setLayout(self.lay)
self.scrollArea.setWidget(self.w)
if __name__ == '__main__':
app = QApplication(sys.argv)
eg = Example()
eg.show()
app.exec_()
Вариант 3 (последний)
import sys
import os
import csv
#from PySide.QtCore import *
#from PySide.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Example(QMainWindow):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.dirPath = "E:/_Qt/Python-Examples/_PyQt5/Test"
self.filePath = ""
self.scrollArea = QScrollArea()
self.scrollArea.setBackgroundRole(QPalette.Light)
self.scrollArea.setWidgetResizable(True)
self.mainLayout = QGridLayout()
self.mainLayout.addWidget(self.scrollArea,0,0)
self.setCentralWidget(QWidget(self))
self.centralWidget().setLayout(self.mainLayout)
self.setGeometry(100,100,800,500)
self.open_file()
def open_file(self):
self.w = QWidget()
self.lay = QVBoxLayout()
self.model = QStandardItemModel(self)
self.tableView = QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
self.tableView.clicked[QModelIndex].connect(self.onClick)
self.appendRowItems(self.dirPath)
self.lay.addWidget(self.tableView)
self.w.setLayout(self.lay)
self.scrollArea.setWidget(self.w)
def appendRowItems(self, dir):
for root, dirs, files in os.walk(dir):
if root == dir:
for file in files:
self.model.appendRow(QStandardItem(file))
def onClick(self, ind):
self.filePath = self.dirPath +"/"+ self.model.item(ind.row(), ind.column()).text()
self.see_file(self.filePath)
def see_file(self, file):
if self.lay.count() > 1:
self.tableView2.close()
self.lay.removeWidget(self.tableView2)
self.model2 = QStandardItemModel(self)
self.tableView2 = QTableView(self)
self.tableView2.setModel(self.model2)
self.tableView2.horizontalHeader().setStretchLastSection(True)
try:
with open(file, "r") as fileInput:
for row in csv.reader(fileInput):
items = [
QStandardItem(field)
for field in row
]
if items: self.model2.appendRow(items)
except Exception as err:
QMessageBox.warning(self, 'Error', str(err))
self.lay.addWidget(self.tableView2)
self.w.setLayout(self.lay)
if __name__ == '__main__':
app = QApplication(sys.argv)
eg = Example()
eg.show()
app.exec_()