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

时间:2019-03-16 20:16:47

标签: python-3.x dictionary pyqt5 qtablewidget qtablewidgetitem

我从我的项目中提取了一个数据字典,我想从不同的列表中提取第二个范围内的项目作为字典的值,我尝试了某人在该论坛中建议的方法“感谢您救命!”这次我想从每个键中以345 #####开头选择项目,其余的####(注意:所选项目的最大数量为20),循环开始选择项目并创建总和,如果没在下一位置找到要保存总和的键,并将其插入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_())

1 个答案:

答案 0 :(得分:1)

尝试一下:

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_())

enter image description here