[Python] How to highlight rows in treeview GUI [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    def update_qc_status_and_comment():
    try:
    # Get the selected item in both TreeViews
    selected_item_daily = tree_daily.selection()
    selected_item_monthly = tree_weekly.selection()

    if selected_item_daily:
    selected_item = selected_item_daily[0] # If daily row is selected
    tree_view = tree_daily
    elif selected_item_monthly:
    selected_item = selected_item_monthly[0] # If monthly row is selected
    tree_view = tree_weekly
    else:
    messagebox.showwarning("Warning", "Please select a row to update.")
    return

    # Get the row values of the selected item
    row_values = tree_view.item(selected_item, 'values')
    print(f"Row values before update: {row_values}") # Debugging line
    print(f"Number of columns in selected row: {len(row_values)}") # Debugging

    # **Ensure the row has at least 13 columns**
    updated_values = list(row_values) # Convert tuple to list

    while len(updated_values) < 13: # If missing columns, extend list
    updated_values.append("") # Fill missing columns with empty strings

    # **Update QC Status**
    updated_values[11] = qc_status_var.get() # Update Visual QC Approved (index 11)
    print(f"Updated QC Status: {updated_values[11]}") # Debugging line

    # **Update Comment Column (Index 12)**
    new_comment = comment_textbox.get().strip()
    if new_comment:
    existing_comment = updated_values[12] if updated_values[12] else ""
    updated_values[12] = f"{existing_comment}, {new_comment}" if existing_comment else new_comment

    # **Update the TreeView row**
    tree_view.item(selected_item, values=updated_values)

    # **Apply Highlight Based on QC Status**
    qc_status = updated_values[11]
    if qc_status == "Pending":
    tree_view.item(selected_item, tags=("Pending",))
    elif qc_status == "Approved":
    tree_view.item(selected_item, tags=("Approved",))
    elif qc_status == "Rejected":
    tree_view.item(selected_item, tags=("Rejected",))

    # **Debugging: Print the assigned tags**
    print(f"Tags for row {selected_item}: {tree_view.item(selected_item, 'tags')}")

    # **Update Google Sheet**
    serial_number = row_values[6] # Assuming serial number is at index 6
    all_values = sheet.get_all_values() # Get all values from the sheet

    for i, row in enumerate(all_values):
    if row[6] == serial_number:
    sheet.update_cell(i + 1, 12, updated_values[11]) # Update Visual QC Approved (column 12)
    sheet.update_cell(i + 1, 13, updated_values[12]) # Update Comments (column 13)
    break

    # **Reset Fields**
    qc_status_var.set('')
    comment_textbox.set('')

    messagebox.showinfo("Success", "QC status and comment updated successfully.")

    except Exception as e:
    messagebox.showerror("Error", f"An error occurred: {str(e)}")


    When the Visual QC is approved it need to be green when it's pending it need to be yellow and if rejected it need to be red.

    Continue reading...

Compartilhe esta Página