这是我的项目的摘录。 我想通过此方法updateActifData()从数据字典中的每个键中获取项目。通常,当我调用它时,它将传递第一个参数以生成第一项,第二个参数将生成第二项....当我从第一个键获取该项时,我想将其插入相应的QTableWidget的单元格中(n,m),对于n = 18,K =“ 234”,并对所有键进行处理。但是我的问题是,当我调用updateActifData()时,它显示了最后一个单元格中所有键的总和。如果您有任何建议,谢谢。
data = {'2111':['Capital 1','145321','94565','','','','74651','','','24651','','',''], '23411':['Capital 2','65115','','6149','6645','555641','','','','41245','98416','',''], '23421':['Capital 3','54445','','45c4545','','555641','434556','','453453','','98416','','45453'], '3421':['Capital 4','714445','','34545','155485','','894556','','15645','26149','98416','',''], '3422':['Capital 5','87676','','54553','466149','','','95436','','','76745','','74568',''], '3451':['Capital 6','45645','','','47679','17345','555641','45256','','','412045','98416','',''], '2230':['Capital 7','453445','74345','','466149','44645','1','98656','12564','','412045','98416','',''], '3452':['Capital 8','97845c4','','','1326149','14553','555641','','45345','86945','412045','98416','','']}
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.setWindowTitle("QTableWidgetItem")
self.resize(1200, 800)
conLayout = QHBoxLayout()
self.tableWidget_Bl =QTableWidget(self)
self.tableWidget_Bl.setRowCount(55)
self.tableWidget_Bl.setColumnCount(14)
conLayout.addWidget(self.tableWidget_Bl)
self.setLayout(conLayout)
self.updateItems()
def updateItems(self):
m = 5
#if self.data:
self.r.setData()
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.getb_2Data())))
self.tableWidget_Bl.setItem(int(self.r.getrow()), m-2, QTableWidgetItem(str(self.r.getb_1Data())))
return self.r.setData()
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)
self.item_n, self.item_b_1, self.item_b_2 = 0, 0, 0
self.n = 0
self.setData()
def setData(self):
self.updateActifData(k="234", n=18)
self.updateActifData(k="342", n=39)
self.updateActifData(k="345", n=41)
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
v=5
for kd, vd in self.data.items():
if kd[:len(k)] == k:
s_1 = vd[v]
print("{:<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("{:<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
for kd, vd in self.data.items():
if kd[:len(k)] == k:
s_1 = vd[v+6]
print("{:<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+7] # the next range
print("{:<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_2 = item_1 + item_2
self.item_n = self.item_b_1 - self.item_b_2
def getrow (self):
print ("getrow:", self.n)
return self.n
def getb_1Data (self):
return self.item_b_1
def getb_2Data (self):
return self.item_b_2
def getnData (self):
return self.item_n
if __name__ == '__main__':
app = QApplication(sys.argv)
windows = Table()
windows.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
尝试一下:
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", 18), ("342", 39), ("345", 41)) # <-----------
for k, n in runItem: # <-----------
self.r.updateActifData( k, n )
self.tableWidget_Bl.setItem(int(self.r.getrow()), m, QTableWidgetItem(str(self.r.getnData())))
m -= 1
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
# 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
self.item_n = self.item_b_1 - self.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
if __name__ == '__main__':
app = QApplication(sys.argv)
windows = Table()
windows.setWindowTitle("QTableWidgetItem")
windows.resize(1200, 800)
windows.show()
sys.exit(app.exec_())