使用 PyQt5“更新”从具有多个参数的函数中销毁返回值 [关闭]

Posted

技术标签:

【中文标题】使用 PyQt5“更新”从具有多个参数的函数中销毁返回值 [关闭]【英文标题】:Destroy a return value from a function with multiple arguments using PyQt5 "Updated" [closed] 【发布时间】:2019-04-04 22:09:14 【问题描述】:

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

【问题讨论】:

【参考方案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, 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_())

【讨论】:

成功了,兄弟 如何在单元格中插入 item_b_1 (n, m-1)? @AnassBelmaati 在循环末尾for k, n in runItem: 插入行m -= 1 如果,我添加了 m -=1,它只适用于 item_n,我更新代码 谢谢兄弟,我找到了问题所在,我只是在获得值之前将赋值 self.item_b_1 = 0 放入该函数中。学习失败

以上是关于使用 PyQt5“更新”从具有多个参数的函数中销毁返回值 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

信号获取多个 Qcombox 的多个内容作为函数中的多个参数

pyqt5 继承python类可以传递信号函数吗

具有多个意图的 PyQt5(同一窗口中的页面)

PYQT5 - 插槽函数不采用默认参数 [重复]

如何使用 ComponentRef 从内部销毁我的组件?

如何从 Matlab 调用具有多个输出的 C++ 函数?