This is a code proposed by S.N all the thanks to him, using functional Programming. It had a serious problem. If it gives runItem tuple some element for example ("222", 15), and this element didn't exist on the data dictionary, it's reference to the previous values duo to this method getbrutActifData() return. In fact, i want it to be a "0". Normally, the runItem for my program is a static asset contains all the keys that will suppose to be in a general data. But the dictionary will change depend on the data. This is the code if you have any suggestion, and thank you. I update the code to inserting item_b_1 in cell (n, -1) with "0", if didn't exist in the dict.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
data = {'2111' :['Capital 1','1','1', '', '', '', '1', '', '', '1','', '', ''],
'23411':['Capital 2','2', '', '2','2','21','21','', '', '2','2','23','24'],
'23421':['Capital 3','3', '', '3','', '31','32','', '3','', '3','33','34'],
'3421' :['Capital 4','4', '', '4','4','41','42','', '4','4','4','43','44'],
'3422' :['Capital 5','5', '', '5','5','51','52','5','', '', '5','53','54'],
'3451' :['Capital 6','6', '', '', '6','61','62','6','', '', '6','63','64'],
'2230' :['Capital 7','7', '7','', '7','7', '7', '7','7','', '7','7', ''],
'3452' :['Capital 8','8', '', '', '8','81','82','', '8','8','8','83','84']}
class Table(QWidget):
def __init__(self, *args, parent=None):
super().__init__()
self.data = data
self.item_n, self.item_b_1, self.item_b_2 = 0, 0, 0
self.r = BlData(self.data, self.item_n, self.item_b_1, self.item_b_2)
self.setuptUI()
def setuptUI(self):
self.tableWidget_Bl = QTableWidget(55, 14, self)
conLayout = QHBoxLayout()
conLayout.addWidget(self.tableWidget_Bl)
self.setLayout(conLayout)
self.updateItems()
def updateItems(self):
m = 5
runItem = (("234", 10), ("222", 15), ("235", 15), ("342", 17), ("345", 19))
for k, n in runItem:
self.r.updateActifData( k, n )
self.tableWidget_Bl.setItem(int(self.r.getrow()), m, QTableWidgetItem(str(self.r.getnData())))
self.tableWidget_Bl.setItem(int(self.r.getrow()), m-1, QTableWidgetItem(str(self.r.getitem_1Data())))
class BalanceData():
data = {}
def __init__(self, data):
self.data = data
class BlData(BalanceData):
def __init__ (self, data, item_n, item_b_1, item_b_2):
BalanceData.__init__(self, data)
def updateActifData(self, k, n):
#the sum for one key
self.n = n
self.item_n, self.item_b_1, self.item_b_2, item_1, item_2 = 0, 0, 0, 0, 0 # +++
item_3, item_4 = 0, 0
v = 5
for kd, vd in self.data.items():
if kd[:len(k)] == k:
s_1 = vd[v]
print("\n1 {:<10} -> {:10.2f}".format(kd, float(s_1) if s_1 else 0))
item_1 = item_1 + (float(s_1) if s_1 else 0)
#print(item)
s_2 = vd[v+1] # the next range
print("2 {:<10} -> {:10.2f}".format(kd, float(s_2) if s_2 else 0))
item_2 = item_2 + (float(s_2) if s_2 else 0)
item_b_1 = item_1 + item_2
for kd, vd in self.data.items():
if kd[:len(k)] == k:
s_1 = vd[v+6]
print("3 {:<10} -> {:10.2f}".format(kd, float(s_1) if s_1 else 0))
item_3 = item_3 + (float(s_1) if s_1 else 0)
#print(item)
s_2 = vd[v+7] # the next range
print("4 {:<10} -> {:10.2f}".format(kd, float(s_2) if s_2 else 0))
item_4 = item_4 + (float(s_2) if s_2 else 0)
item_b_2 = item_3 + item_4
self.item_n = item_b_1 - item_b_2
print("item_n= `{}`, item_b_1= `{}`, item_b_2= `{}`"
"".format(self.item_n, self.item_b_1, self.item_b_2))
def getrow (self):
print ("getrow:", self.n)
return self.n
def getnData (self):
return self.item_n
def getitem_1Data():
return self.item_b_1
if __name__ == '__main__':
app = QApplication(sys.argv)
windows = Table()
windows.setWindowTitle("QTableWidgetItem")
windows.resize(1200, 800)
windows.show()
sys.exit(app.exec_())
Try it:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
data = {'2111' :['Capital 1','1','1', '', '', '', '1', '', '', '1','', '', ''],
'23411':['Capital 2','2', '', '2','2','21','21','', '', '2','2','23','24'],
'23421':['Capital 3','3', '', '3','', '31','32','', '3','', '3','33','34'],
'3421' :['Capital 4','4', '', '4','4','41','42','', '4','4','4','43','44'],
'3422' :['Capital 5','5', '', '5','5','51','52','5','', '', '5','53','54'],
'3451' :['Capital 6','6', '', '', '6','61','62','6','', '', '6','63','64'],
'2230' :['Capital 7','7', '7','', '7','7', '7', '7','7','', '7','7', ''],
'3452' :['Capital 8','8', '', '', '8','81','82','', '8','8','8','83','84']}
class Table(QWidget):
def __init__(self, data): #*args, parent=None):
super().__init__()
self.data = data
self.item_n, self.item_b_1, self.item_b_2 = 0, 0, 0
self.r = BlData(self.data, self.item_n, self.item_b_1, self.item_b_2)
self.setuptUI()
def setuptUI(self):
self.tableWidget_Bl = QTableWidget(55, 14, self)
conLayout = QHBoxLayout()
conLayout.addWidget(self.tableWidget_Bl)
self.setLayout(conLayout)
self.updateItems()
def updateItems(self):
m = 5
runItem = (("234", 10), ("222", 15), ("235", 16), ("342", 17), ("345", 19))
for k, n in runItem:
self.r.updateActifData( k, n )
self.tableWidget_Bl.setItem(int(self.r.getrow()), m, QTableWidgetItem(str(self.r.getnData())))
class BalanceData():
data = {}
def __init__(self, data):
self.data = data
class BlData(BalanceData):
def __init__ (self, data, item_n, item_b_1, item_b_2):
BalanceData.__init__(self, data)
def updateActifData(self, k, n):
#the sum for one key
self.n = n
item_n, item_b_1, item_b_2, item_1, item_2 = 0, 0, 0, 0, 0
item_3, item_4 = 0, 0
v = 5
for kd, vd in self.data.items():
if kd[:len(k)] == k:
s_1 = vd[v]
print("\n1 {:<10} -> {:10.2f}".format(kd, float(s_1) if s_1 else 0))
item_1 = item_1 + (float(s_1) if s_1 else 0)
#print(item)
s_2 = vd[v+1] # the next range
print("2 {:<10} -> {:10.2f}".format(kd, float(s_2) if s_2 else 0))
item_2 = item_2 + (float(s_2) if s_2 else 0)
# self.item_b_1 = item_1 + item_2
item_b_1 = item_1 + item_2 # +++
# for kd, vd in self.data.items():
# if kd[:len(k)] == k:
s_1 = vd[v+6]
print("3 {:<10} -> {:10.2f}".format(kd, float(s_1) if s_1 else 0))
item_3 = item_3 + (float(s_1) if s_1 else 0)
#print(item)
s_2 = vd[v+7] # the next range
print("4 {:<10} -> {:10.2f}".format(kd, float(s_2) if s_2 else 0))
item_4 = item_4 + (float(s_2) if s_2 else 0)
# self.item_b_2 = item_3 + item_4
item_b_2 = item_3 + item_4 # +++
# self.item_n = self.item_b_1 - self.item_b_2
self.item_n = item_b_1 - item_b_2 # +++
print("item_n= `{}`, item_b_1= `{}`, item_b_2= `{}`"
"".format(self.item_n, item_b_1, item_b_2)) # +++
# "".format(self.item_n, self.item_b_1, self.item_b_2))
def getrow (self):
print ("getrow:", self.n)
return self.n
def getnData (self):
return self.item_n
if __name__ == '__main__':
app = QApplication(sys.argv)
windows = Table(data) # data
windows.setWindowTitle("QTableWidgetItem")
windows.resize(1200, 800)
windows.show()
sys.exit(app.exec_())