Search code examples
pythontkintertreeviewget-childitem

How to write the if clause when iterating through a treeview?


I have a function that checks if a barcode is known to the warehouse. If so, the function grabs the row from the dataframe (occupied by an imported excel file) and will be inserted into a treeview with known items. If the barcode is unknown it will be inserted into a listbox.

The function works and does what it is supposed to do, but I want to expand it by updating the row in the treeview by increasing its quantity by 1 when adding the same barcode to the treeview. See the picture for the current behaviour. Known items treeview

# Function to process new entered barcodes by filtering known and unknown items and adding them to treeview
def scan_check(event):
    scanned_item = scan_entry.get()
    for code in df.iloc[:, 1]:  # column with barcodes
        if code == scanned_item:
            for row in df.to_numpy().tolist():  # dataframe with item / barcode / item description / size / quantity
                if scanned_item in row:
                    quantity_count = 1
                    row.insert(4, quantity_count)
                    scanTree.insert(parent='', index='end', value=row)
                    for child in scanTree.get_children():
                        if scanTree.item(child, option='values'[3]) in scanTree.get_children():
                            quantity_count += 1
                            scanTree.set(child, 'Quantity', quantity_count)
                  
            scan_entry.delete(0, tkinter.END)

            break   # to prevent adding item to unknown products listbox as well

    else:
        unknown_listbox.insert(tkinter.END, scanned_item)
        scan_entry.delete(0, tkinter.END)

My question is: How would I write the if clause, after iterating throught the children, when I want to check if the added row from the dataframe is already in my treeview?

My attempts at the if clause did not work obviously. I was hoping anyone could help me with my problem. Thanks for reading.


Solution

  • You can simplify the logic:

    • Search the treeview first for the barcode, if found, update the quantity
    • If not found, search the dataframe. If found, insert new record into treeview, otherwise insert the barcode to the unknown listbox
    def scan_check(event):
        scanned_item = scan_entry.get().strip()
        if scanned_item == '':
            # do nothing if empty string is input
            return
    
        # search treeview
        for child in scanTree.get_children():
            row = scanTree.set(child)
            if row['Barcode'] == scanned_item:
                # update quantity
                scanTree.set(child, 'Quantity', int(row['Quantity'])+1)
                break # prevent executing else block
        else:
            # search dataframe
            result = df.loc[df['Barcode'].astype(str) == scanned_item]
            if result.empty:
                # should check whether barcode already exists?
                unknown_listbox.insert('end', scanned_item)
            else:
                scanTree.insert('', 'end', values=result.iloc[0].to_list()+[1])
        scan_entry.delete(0, 'end')