Tkinter / Python:list1更新,即使我不应该将list2与list1进行比较时也不应该更新

时间:2018-01-10 00:16:08

标签: python list tkinter reference

当我在updateCustomerList函数下的代码中更新/触摸它时,为什么更新/更改masterList中的索引时,我感到很困惑。我只将它用作对我的customerList的引用。只有当我使用负数量值来基本删除或删除框架中的对象时才会这样做

refreshMasterList()只允许我刷新帧,因此框架将由customerList内更新的新数据填充。

注意:如果缩进似乎有误,请告诉我。我还创建了一个gist来查看整个程序

如果您想运行该程序并亲自检查一下,只需下载此test excel doc并使用 3605520297939 作为条形码

def updateCustomerList(barCode, quantity):
    global customerList

    if not barCode.get():
        return  
    else:

        for i in range(len(masterList)):

            if int(barCode.get()) == masterList[i][4]:

                if len(customerList) == 0:
                    customerList.append(masterList[i])
                    customerList[0].append(quantity.get())

                    print("Customer list is empty, before delete " + str(customerList) + "         M " + str(masterList[i]))

                    if customerList[0][5] <= 0:
                        del customerList
                        customerList = []

                    print("Customer list is empty" + str(customerList) + "   M " + str(masterList[i]))

                    refreshMainFrame()

                else:

                    print("~CustomerList is not empty " + str(masterList[i]))

                    for j in range(len(customerList)):
                        if int(barCode.get()) == customerList[j][4]:
                            intQuantity = customerList[j][5] + quantity.get()

                            if intQuantity <= 0:
                                del customerList[j]

                            print("~~Customer list is not empty but w/ repeated bar code" + str(customerList) + "   M " + str(masterList[i]))

                            refreshMainFrame()
                            return

                    customerList.append(masterList[i])  #adds a masterList object inside customerList
                    customerList[len(customerList)-1].append(quantity.get())   

                    if customerList[len(customerList)-1][5] <= 0:
                        del customerList[len(customerList)-1]

                    print("~~~Customer list is not empty" + str(customerList) + "   " + str(masterList[i]))

                    refreshMainFrame()

    refreshMainFrame()
    return

1 个答案:

答案 0 :(得分:0)

您正在修改masterList内的customerList可变项:

customerList.append(masterList[i])
customerList[0].append(quantity.get())   #<-- for instance here
customerList.append(masterList[i])
customerList[len(customerList)-1].append(quantity.get()) #<-- and here too

由于customerList仅包含对已修改项目的引用,因此两个位置都可以看到更改。

在操作它们之前,您可以尝试对从masterList中提取的每个项目进行硬拷贝:

customerList.append(masterList[i][:])    #<-- for instance do a hard copy here
customerList[0].append(quantity.get())