显示多个项目的总和
Posted
技术标签:
【中文标题】显示多个项目的总和【英文标题】:Show the sum for multiple items 【发布时间】:2019-03-15 22:00:06 【问题描述】:我有一个数据字典,我想在第二个范围内提取一些项目,例如从不同的列表中作为字典的值,每个键都以 111#### 开头,我创建了这个方法,但是如何,我可以得到将所有项目的总和插入 QTableWidget 单元格?
import sys
from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem)
from PyQt5.QtGui import QBrush, QColor
from PyQt5 import QtCore
data = '1111':['Title 1','12521','94565','','','','684651','','','44651','','',''], '1112':['Title 2','65115','','466149','46645','555641','','','','412045','98416','',''], '1113':['Title 3','243968','','','466149','46645','555641','98656','','','412045','98416','','']
class Table(QWidget):
def __init__(self, *args, parent=None):
super().__init__()
self.data = data
self.setupUi()
def setuptUI(self):
self.setWindowTitle("QTableWidgetItem")
self.resize(1200, 800)
conLayout = QHBoxLayout()
self.tableWidget =QTableWidget(self)
self.tableWidget.setRowCount(55)
self.tableWidget.setColumnCount(14)
conLayout.addWidget(self.tableWidget)
def setdata(self, k, v, n, m):
i= 1
l = str(k*10)
while I < 4
item = self.data.get(str(int(l)+i))[v]
print(item)
i= i+1
if v== " ":
break
#sum_instruction
newItem = QTableWidgetItem(str(item))
newItem.setForeground(QBrush(QColor(255, 0, 0)))
self.tableWidget.setItem(m, n, newItem)
if __name__ == '__main__':
app = QApplication(sys.argv)
windows = Table(data)
windows.setdata(k="113", v=5, n=25, m=4
windows.show()
sys.exit(app.exec_())
【问题讨论】:
【参考方案1】:试试看:
import sys
from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem)
from PyQt5.QtGui import QBrush, QColor
from PyQt5 import QtCore
# vvvvvv
data = '1111':['Title 1','12521', '94565','', '', '777777','684651','','','44651','','',''],
'1112':['Title 2','65115', '', '466149','46645', '555641','','','','412045','98416','',''],
'1113':['Title 3','243968','', '', '466149','46645', '555641','98656','','','412045','98416','','']
# ^^^^^^
class Table(QWidget):
def __init__(self, data):
super().__init__()
self.data = data
self.setupUi()
def setupUi(self):
conLayout = QHBoxLayout(self) # self
self.tableWidget = QTableWidget(self)
self.tableWidget.setRowCount(55)
self.tableWidget.setColumnCount(14)
conLayout.addWidget(self.tableWidget)
def setdata(self, k, v, n, m):
print(k, v, n, m)
i = 1
# l = str(k*10)
l = str(int(k)*10)
s = 0
while i < 4:
item = self.data.get(str(int(l)+i))[v]
print(item)
i = i+1
# if v == " ":
# break
#sum_instruction
s = s + (int(item) if item else 0)
newItem = QTableWidgetItem(str(item))
newItem.setForeground(QBrush(QColor(255, 0, 0)))
self.tableWidget.setItem(m, n, newItem)
n += 1 # +++
newItem = QTableWidgetItem(str(s))
newItem.setForeground(QtCore.Qt.blue)
self.tableWidget.setItem(m, n, newItem)
if __name__ == '__main__':
app = QApplication(sys.argv)
windows = Table(data)
# windows.setdata(k="113", v=5, n=25, m=4)
windows.setdata(k="111", v=5, n=2, m=4)
windows.setWindowTitle("QTableWidgetItem")
windows.resize(1100, 700)
windows.show()
sys.exit(app.exec_())
【讨论】:
S.nick 我尝试了你的方法,它可以找到但我想在 i 时使用动态 int 循环以上是关于显示多个项目的总和的主要内容,如果未能解决你的问题,请参考以下文章
SQL从日期范围内的同一表中的不同记录中获取多个项目的总和(ORACLE)
如何在 react.js 中显示同一项目和不同日期的值的总和
R shinydashboard 在值框中显示所选输入的总和