显示动态数字的多个项目的总和

Show the sum for multiple items for a dynamic numbers

我有一个从我的项目中提取的数据字典,我想从不同的列表中选择第二个范围的项目作为字典的值,我尝试了这个论坛中某人建议的方法 "Thank u for every help!" 这次我想 select 每个键的项目都以 345##### 开头,其余的 #### (注意:selected 项目最多为 20),循环开始到 select 项目并创建一个总和,如果在下一个位置找不到密钥,它将保存总和,并将其插入 QtableWidget 单元格,我尝试了不同的方式:/请提出任何建议。

import sys
from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem)
from PyQt5.QtGui import QBrush, QColor 
from PyQt5 import QtCore
#from FixalSolutions_build_1_3 import Ui_MainWindow


data = {'1111':['Capital 1','145321','94565','','','','74651','','','24651','','',''], '1112':['Capital 2','65115','','6149','6645','555641','','','','41245','98416','',''], '1113':['Capital 3','544453','','45345453','','555641','434556','','453453','','98416','','453453'], '1114':['Capital 4','7144453','','345453','3155485','','894556','','156453','326149','98416','',''], '1121':['Capital 5','87676','','534553','466149','','','95436','','','76745','','74568',''], '1122':['Capital 6','45645','','','47679','17345','555641','345256','','','412045','98416','',''], '1123':['Capital 7','4534453','74345','','466149','44645','1','98656','12564','','412045','98416','',''], '1124':['Capital 8','9784534','','','1326149','14553','555641','','45345','445345','412045','98416','',''], '1161': ['RAN SC', '', '', '', '32412.8', '', '32412.8', '', '', '', '', '', ''], '1162': ['Compte', '', '49850.22', '', '', '', '49850.22', '', '', '', '49850.22', '', '49850.22'], '1169': ['RAN SD', '22744.59', '', '', '', '22744.59', '', '', '', '22744.59', '', '22744.59', ''], '3455': ['item 1', '9880.4', '', '', '', '9880.4', '', '', '', '9880.4', '', '9880.4', ''], '34550200': [ 'item 2', '681.66', '', '', '', '681.66', '', '', '', '681.66', '', '681.66', ''], '34552010': [ 'item 3', '1083.87', '', '', '', '1083.87', '', '', '', '1083.87', '', '1083.87', ''], '34552020': [ 'item 4', '', '', '38026.61', '34080.67', '3945.94', '', '', '', '', '', '', ''], '34552140': [ 'item 5', '17358.79', '', '', '', '17358.79', '', '', '', '17358.79', '', '17358.79', ''], '34552200': [ 'item 6', '', '28037.7', '', '', '', '28037.7', '', '', '', '28037.7', '', '28037.7']}

class Table(QWidget):
    def __init__(self, *args, parent=None):
        super().__init__()
        self.data = data
        self.setuptUI()

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)
    item = 0
    while i < i+1
            if l in self.data
                  s = self.data.get(str(int(l)+i))[v]
                  i= i+1
                  print(item)
                  item = item + (int(s) if s else 0)
             else:
                  break

    self.tableWidget_Bilan.setItem(n, m, QTableWidgetItem(str(item)))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    windows = Table(data)
    windows.setdata(k="345", v=5, n=25, m=4
    windows.show()
    sys.exit(app.exec_())

试一试:

import sys
from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, 
    QApplication, QTableWidgetItem)
from PyQt5.QtGui import QBrush, QColor 
from PyQt5 import QtCore
#from FixalSolutions_build_1_3 import Ui_MainWindow


class Table(QWidget):
    def __init__(self, data):
        super().__init__()
        self.data = data
        self.setuptUI()

    def setuptUI(self):
        self.setWindowTitle("QTableWidgetItem")
        self.resize(1200, 600)

        self.tableWidget =QTableWidget(self)
        self.tableWidget.setRowCount(55)
        self.tableWidget.setColumnCount(14)
        conLayout = QHBoxLayout(self)               # + self
        conLayout.addWidget(self.tableWidget)

    def setdata(self, k, v, r, c):
        item = 0
        for kd, vd in self.data.items():
            if kd[:len(k)] == k:
                s = vd[v]
                print("{:<10} -> `{: 10.2f}`".format(kd, float(s) if s else 0))
                item = item + (float(s) if s else 0)

        self.tableWidget.setItem(r, c, QTableWidgetItem(str(item)))



data = {'1111':['Capital 1','145321','94565','','','','74651','','','24651','','',''], 
        '1112':['Capital 2','65115','','6149','6645','555641','','','','41245','98416','',''], 
        '1113':['Capital 3','544453','','45345453','','555641','434556','','453453','','98416','','453453'], 
        '1114':['Capital 4','7144453','','345453','3155485','','894556','','156453','326149','98416','',''], 
        '1121':['Capital 5','87676','','534553','466149','','','95436','','','76745','','74568',''], 
        '1122':['Capital 6','45645','','','47679','17345','555641','345256','','','412045','98416','',''], 
        '1123':['Capital 7','4534453','74345','','466149','44645','1','98656','12564','','412045','98416','',''], 
        '1124':['Capital 8','9784534','','','1326149','14553','555641','','45345','445345','412045','98416','',''], 
        '1161': ['RAN SC', '', '', '', '32412.8', '', '32412.8', '', '', '', '', '', ''], 
        '1162': ['Compte', '', '49850.22', '', '', '', '49850.22', '', '', '', '49850.22', '', '49850.22'], 
        '1169': ['RAN SD', '22744.59', '', '', '', '22744.59', '', '', '', '22744.59', '', '22744.59', ''], 

        '3455': ['item 1', '9880.4', '', '', '',                '9880.4',   '', '', '', '9880.4', '', '9880.4', ''], 
        '34550200': [ 'item 2', '681.66', '', '', '',           '681.66',   '', '', '', '681.66', '', '681.66', ''], 
        '34552010': [ 'item 3', '1083.87', '', '', '',          '1083.87',  '', '', '', '1083.87', '', '1083.87', ''], 
        '34552020': [ 'item 4', '', '', '38026.61', '34080.67', '3945.94',  '', '', '', '', '', '', ''], 
        '34552140': [ 'item 5', '17358.79', '', '', '',         '17358.79', '', '', '', '17358.79', '', '17358.79', ''], 
        '34552200': [ 'item 6', '', '28037.7', '', '',          '',         '28037.7', '', '', '', '28037.7', '', '28037.7']}


if __name__ == '__main__':
    app = QApplication(sys.argv)
    windows = Table(data)
    windows.setdata(k="345", v=5, r=25, c=4)
    windows.show()
    sys.exit(app.exec_())