我正在编写一个简单的csv处理程序,它将仅使用python 3.7.2和pyqt5查看两列。它将具有所需的行数,并且所有这些行都需要填充才能使我的程序正常工作。但是当我运行循环时,20个单元中只有11个被填充。当我将行数减少到10时,该比率为6/10,而对于6,则为4/6。我找不到原因。
这里是首先设置标头的循环。
x = 0
while x<total_rows:
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(x, item)
x+=1
这是用零填充第一列的循环。
x = 0
while x<total_rows:
print(x)
self.tableWidget.setItem(0,x, QtWidgets.QTableWidgetItem("0"))
x+=1
这是用零填充第二列的循环。
x = 0
while x<total_rows:
print(x)
self.tableWidget.setItem(1,x, QtWidgets.QTableWidgetItem("0"))
x+=1
有关解决我的问题时可能需要的其他详细信息,这里是整个代码。 (我目前没有可用的文件共享平台。请接受我的歉意,以文本形式共享整个代码。)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'database.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
import os
from pandas import DataFrame, read_csv
import pandas as pd
from datetime import datetime
from PyQt5 import QtCore, QtGui, QtWidgets
total_rows = 20
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(551, 602)
#Layout
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setObjectName("verticalLayout")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setMaximumSize(QtCore.QSize(16777215, 70))
self.widget.setStyleSheet("background-color: black;")
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.widget)
self.label.setStyleSheet("color: white;\nfont-size: 36px;")
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
spacerItem = QtWidgets.QSpacerItem(337, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.verticalLayout.addWidget(self.widget)
self.widget_2 = QtWidgets.QWidget(self.centralwidget)
self.widget_2.setStyleSheet("background-color: #777;")
self.widget_2.setObjectName("widget_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.widget_2)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.widget_3 = QtWidgets.QWidget(self.widget_2)
self.widget_3.setStyleSheet("background-color: #556;")
self.widget_3.setObjectName("widget_3")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.widget_3)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_3.setSpacing(0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.widget_5 = QtWidgets.QWidget(self.widget_3)
self.widget_5.setStyleSheet("background-color:white;")
self.widget_5.setObjectName("widget_5")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget_5)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
#TableWidget
self.tableWidget = QtWidgets.QTableWidget(self.widget_5)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setStyleSheet("background-color: #eee;")
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(total_rows)
x = 0
while x<total_rows:
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(x, item)
x+=1
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
self.horizontalLayout_2.addWidget(self.tableWidget)
#CalendarWidget
self.calendarWidget = QtWidgets.QCalendarWidget(self.widget_5)
self.calendarWidget.setStyleSheet("color:black;")
self.calendarWidget.setObjectName("calendarWidget")
self.horizontalLayout_2.addWidget(self.calendarWidget)
self.verticalLayout_3.addWidget(self.widget_5)
self.widget_4 = QtWidgets.QWidget(self.widget_3)
self.widget_4.setStyleSheet("background-color: white;")
self.widget_4.setObjectName("widget_4")
self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.widget_4)
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.widget_8 = QtWidgets.QWidget(self.widget_4)
self.widget_8.setObjectName("widget_8")
self.verticalLayout_5.addWidget(self.widget_8)
self.widget_9 = QtWidgets.QWidget(self.widget_4)
self.widget_9.setObjectName("widget_9")
self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.widget_9)
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_6.addItem(spacerItem1)
#PushButton
self.pushButton = QtWidgets.QPushButton(self.widget_9)
self.pushButton.setStyleSheet("background-color: black;\ncolor: white;")
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_6.addWidget(self.pushButton)
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_6.addItem(spacerItem2)
self.verticalLayout_5.addWidget(self.widget_9)
self.verticalLayout_3.addWidget(self.widget_4)
self.verticalLayout_2.addWidget(self.widget_3)
self.verticalWidget = QtWidgets.QWidget(self.widget_2)
self.verticalWidget.setStyleSheet("background-color: white;")
self.verticalWidget.setObjectName("verticalWidget")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.verticalWidget)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.widget_7 = QtWidgets.QWidget(self.verticalWidget)
self.widget_7.setObjectName("widget_7")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.widget_7)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
#ListWidget
self.listWidget = QtWidgets.QListWidget(self.widget_7)
self.listWidget.setStyleSheet("background-color:#eee;")
self.listWidget.setObjectName("listWidget")
#Get files and insert into list widget
files = os.listdir("files")
for index, value in enumerate(files):
item = QtWidgets.QListWidgetItem(value)
self.listWidget.addItem(item)
self.horizontalLayout_4.addWidget(self.listWidget)
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_4.addItem(spacerItem3)
self.verticalLayout_4.addWidget(self.widget_7)
self.widget_6 = QtWidgets.QWidget(self.verticalWidget)
self.widget_6.setObjectName("widget_6")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.widget_6)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_3.addItem(spacerItem4)
#PushButton2
self.pushButton_2 = QtWidgets.QPushButton(self.widget_6)
self.pushButton_2.setStyleSheet("background-color:black;\ncolor:white;")
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_3.addWidget(self.pushButton_2)
spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_3.addItem(spacerItem5)
self.verticalLayout_4.addWidget(self.widget_6)
self.verticalLayout_2.addWidget(self.verticalWidget)
self.verticalLayout.addWidget(self.widget_2)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def save_file(self):
#Takvim değerini al
current_date = self.calendarWidget.selectedDate()
current_date_text = current_date.toPyDate().strftime("%d%b%y")
#Table'daki değerleri al
allRows = self.tableWidget.rowCount()
#Pandas table için listeleri oluştur
headers = []
items = []
for row in range(0,allRows):
print(row)
item = self.tableWidget.item(row,1).text()
items.append(item)
item = self.tableWidget.item(row,0).text()
headers.append(item)
#dataset oluştur
data_set = list(zip(headers,items))
#dataframe oluştur
data_frame = pd.DataFrame(data = data_set)
#csv dosyası oluştur
data_frame.to_csv("files/" + current_date_text + ".csv", index = False)
#listview güncelle
files = os.listdir("files")
print(files)
for index, value in enumerate(files):
item = QtWidgets.QListWidgetItem(value)
self.listWidget.addItem(item)
item = self.listWidget.item(index)
item.setText(value)
def open_file(self):
#dosya ismini al
selected_file = self.listWidget.currentItem().text()
#csv dosyasını aç dataframe oluştur
data_frame = pd.read_csv("files/" + selected_file)
items = data_frame["0"].values.tolist()
items = data_frame["1"].values.tolist()
print(items)
for row in range(0,total_rows):
header = row
item = self.tableWidget.setItem(0,row, QtWidgets.QTableWidgetItem(str(items[row])))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
#Set TableWidget
self.label.setText(_translate("MainWindow", "Defter"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Isim"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Miktar"))
x = 0
while x<total_rows:
print(x)
self.tableWidget.setItem(0,x, QtWidgets.QTableWidgetItem("0"))
x+=1
x = 0
while x<total_rows:
print(x)
self.tableWidget.setItem(1,x, QtWidgets.QTableWidgetItem("0"))
x+=1
self.pushButton.setText(_translate("MainWindow", "Kaydet"))
self.pushButton.clicked.connect(self.save_file)
__sortingEnabled = self.listWidget.isSortingEnabled()
self.listWidget.setSortingEnabled(False)
files = os.listdir("files")
for index, value in enumerate(files):
item = self.listWidget.item(index)
item.setText(value)
self.listWidget.setSortingEnabled(__sortingEnabled)
self.pushButton_2.setText(_translate("MainWindow", "Dosyayı aç"))
self.pushButton_2.clicked.connect(self.open_file)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
如果我的循环不正确,我会放置打印语句。我检查了一下,所有循环都运行了20次(每次x值都达到19时)。
编辑:似乎是两位数的问题。但是根据文档,这种语法肯定是正确的。
self.tableWidget.setItem(1,10, QtWidgets.QTableWidgetItem("0"))
答案 0 :(得分:0)
您更改了带有列的行
# ...
x = 0
while x < total_rows:
# --- self.tableWidget.setItem(0, x, QtWidgets.QTableWidgetItem("0"))
self.tableWidget.setItem(x, 0, QtWidgets.QTableWidgetItem("0")) # +++
x += 1
x = 0
while x<total_rows:
# --- self.tableWidget.setItem(1,x, QtWidgets.QTableWidgetItem("0"))
self.tableWidget.setItem(x, 1, QtWidgets.QTableWidgetItem("0")) # +++
x += 1
# ...