使用 PyQt5 "Updated" 从具有多个参数的函数中销毁一个 return 值

Destroy a return value from a function with multiple arguments using PyQt5 "Updated"

这是 S.N 提出的代码,非常感谢他,使用函数式编程。 它有一个严重的问题。如果它给 runItem 元组一些元素,例如 ("222", 15),而这个元素在数据字典中不存在,它引用了这个方法 getbrutActifData() return 之前的值。事实上,我希望它是一个“0”。通常,我的程序的 runItem 是一个静态资产,包含假定在一般数据中的所有键。但是字典会根据数据而改变。如果您有任何建议,这是代码,谢谢。 如果字典中不存在,我将代码更新为在带有“0”的单元格 (n, -1) 中插入 item_b_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", 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_())

试一试:

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