我是一个初学者,需要此代码才能用于PyQt5,但我不知道该怎么做。我尝试过更换零件,但是它崩溃了。它应该能够将CSV文件加载到表格小部件中,并且能够将表格保存到CSV中。
import csv
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4 import QtGui, QtCore
class MyWindow(QtGui.QWidget):
def __init__(self, fileName, parent=None):
super(MyWindow, self).__init__(parent)
self.fileName = fileName
self.model = QtGui.QStandardItemModel(self)
self.tableView = QtGui.QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
self.pushButtonLoad = QtGui.QPushButton(self)
self.pushButtonLoad.setText("Load Csv File!")
self.pushButtonLoad.clicked.connect(self.on_pushButtonLoad_clicked)
self.pushButtonWrite = QtGui.QPushButton(self)
self.pushButtonWrite.setText("Write Csv File!")
self.pushButtonWrite.clicked.connect(self.on_pushButtonWrite_clicked)
self.layoutVertical = QtGui.QVBoxLayout(self)
self.layoutVertical.addWidget(self.tableView)
self.layoutVertical.addWidget(self.pushButtonLoad)
self.layoutVertical.addWidget(self.pushButtonWrite)
def loadCsv(self, fileName):
with open(fileName, "rb") as fileInput:
for row in csv.reader(fileInput):
items = [
QtGui.QStandardItem(field)
for field in row
]
self.model.appendRow(items)
def writeCsv(self, fileName):
with open(fileName, "wb") as fileOutput:
writer = csv.writer(fileOutput)
for rowNumber in range(self.model.rowCount()):
fields = [
self.model.data(
self.model.index(rowNumber, columnNumber),
QtCore.Qt.DisplayRole
)
for columnNumber in range(self.model.columnCount())
]
writer.writerow(fields)
@QtCore.pyqtSlot()
def on_pushButtonWrite_clicked(self):
self.writeCsv(self.fileName)
@QtCore.pyqtSlot()
def on_pushButtonLoad_clicked(self):
self.loadCsv(self.fileName)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow("/path/to/MyFile.csv")
main.show()
sys.exit(app.exec_())
这使用了QTableView,但是我希望它改用Widget。尝试将QWidgets用于pyqt5,但没有运气。
答案 0 :(得分:0)
在这种情况下,使用QTableView
更容易。试试吧:
#import sip
#sip.setapi('QString', 2)
#sip.setapi('QVariant', 2)
#from PyQt4 import QtGui, QtCore
import sys
import csv
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MyWindow(QWidget):
def __init__(self, fileName, parent=None):
super(MyWindow, self).__init__(parent)
self.fileName = fileName
self.model = QStandardItemModel(self)
self.tableView = QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
self.pushButtonLoad = QPushButton(self)
self.pushButtonLoad.setText("Load Csv File!")
self.pushButtonLoad.clicked.connect(self.on_pushButtonLoad_clicked)
self.pushButtonWrite = QPushButton(self)
self.pushButtonWrite.setText("Write Csv File!")
self.pushButtonWrite.clicked.connect(self.on_pushButtonWrite_clicked)
self.layoutVertical = QVBoxLayout(self)
self.layoutVertical.addWidget(self.tableView)
self.layoutVertical.addWidget(self.pushButtonLoad)
self.layoutVertical.addWidget(self.pushButtonWrite)
def loadCsv(self, fileName):
with open(fileName, "r") as fileInput: # - "rb"
for row in csv.reader(fileInput):
items = [
QStandardItem(field)
for field in row
]
self.model.appendRow(items)
def writeCsv(self, fileName):
with open(fileName, "w") as fileOutput: # - "wb"
writer = csv.writer(fileOutput, lineterminator='\n') # + lineterminator='\n'
print('rowCount->', self.model.rowCount())
for rowNumber in range(self.model.rowCount()):
fields = [
self.model.data(
self.model.index(rowNumber, columnNumber),
Qt.DisplayRole
)
for columnNumber in range(self.model.columnCount())
]
print('fields->', fields)
writer.writerow(fields)
@pyqtSlot()
def on_pushButtonWrite_clicked(self):
self.writeCsv(self.fileName)
@pyqtSlot()
def on_pushButtonLoad_clicked(self):
self.loadCsv(self.fileName)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow("D:/_Qt/txt_csv.csv")
main.show()
sys.exit(app.exec_())
txt_csv.csv
a,b,c
1,2,3
4,5,6
7,8,9