问题
I want to write a command for a button that will take all of the information from ALL of the rows (except the title row) in my treeview widget and then later paste it onto a label or preferably a listbox. I don't know how to reference the rows of this widget to pass the info from them especially since my widget can have any number of rows depending on the invoice for that customer. Any help is greatly appreciated.
import datetime as dt
import sqlite3
from tkinter import *
from tkinter import ttk
date = dt.datetime.now()
format_date = f"{date:%a, %b %d %Y}"
f1 = Tk()
f1.title("THE HAT COMPANY")
f1.geometry("750x500")
Invoice_Maker = LabelFrame(f1, text = "Invoice")
Invoice_Maker.grid(row=8,column=0,columnspan=4)
Qty_Label = Label(Invoice_Maker, text = " Qty:")
Qty_Label.grid(row=0,column=0)
Quantity_Box = Entry(Invoice_Maker, width=15)
Quantity_Box.grid(row=0,column=1)
clicked = StringVar()
clicked.set("Select Item")
Dropdown = OptionMenu(Invoice_Maker, clicked, "Bandana", "Du-Rag/Stocking cap",
"Winter Hats", "Gloves", "A-shirt", "Belt",
"Capdana", "Mask", "Sleeve","T-shirt RoundNeck", "T-shirt V-Neck",
"T-shirt LongSleeve","Japanese Hat", "Sunglass", "Small Straw Hat", "Cap",
"Bucket Hat", "Safari Hat", "Large Straw Hat", "Banded Straw Hat",
"Neon T-Shirt", "Panama Jack", "Lady Hats", "Fedora", "Straw Hat Ranchero",
"Team Hats", "Speedy Hat", "Misc", "Bike Tires", "Machetes", "Balones")
Dropdown.grid(row=0, column=2)
Style_Label = Label(Invoice_Maker, text = "Style:")
Style_Label.grid(row = 0, column = 3)
StyleMenu = StringVar()
StyleMenu.set("N/A")
Style = OptionMenu(Invoice_Maker,StyleMenu, "Plain","Camo","Neon")
Style.grid(row=0,column=4)
Cost_Label = Label(Invoice_Maker,text = "Cost:")
Cost_Label.grid(row=0,column=5)
Cost = Entry(Invoice_Maker, width=7)
Cost.grid(row=0, column=6)
Retail_Label = Label(Invoice_Maker,text = "Retail:")
Retail_Label.grid(row=0,column=7)
Retail = Entry(Invoice_Maker, width=7)
Retail.grid(row=0, column=8)
Inv_Frame = LabelFrame(Invoice_Maker, bg="red")
Inv_Frame.grid(row=1,column=0,columnspan=10)
global Trv
Trv = ttk.Treeview(Inv_Frame)
Trv.pack(side=RIGHT)
Trv["columns"] = ("2","3","4","5","6")
Trv.column("#0", width = 40)
Trv.column(1, width = 80)
Trv.column(2, width = 80)
Trv.column(3, width = 80)
Trv.column(4, width = 80)
Trv.column(5, width = 80)
Trv.column(6, width = 80)
Trv.heading("#0", text="#", anchor=W)
Trv.heading(1, text="Quantity", anchor=W)
Trv.heading(2, text="Item", anchor=W)
Trv.heading(3, text="Style", anchor=W)
Trv.heading(4, text="Cost", anchor=W)
Trv.heading(5, text="Retail", anchor=W)
Trv.heading(6, text="Total", anchor=W)
Scroll = Scrollbar(Inv_Frame, orient="vertical", command=Trv.yview)
Scroll.pack(side=LEFT, fill="y")
def Add_Item():
Trv.insert("",END,text=Quantity_Box.get(), values=(clicked.get(),StyleMenu.get(),Cost.get(),
Retail.get(),str(float(Quantity_Box.get())*float(Cost.get()))))
total = sum(float(Trv.set(item, 6)) for item in Trv.get_children())
Total_Box.delete(0, 'end')
Total_Box.insert('end', total)
Quantity_Box.delete(0,END)
Cost.delete(0,END)
Retail.delete(0,END)
clicked.set("Select Item")
StyleMenu.set("N/A")
来源:https://stackoverflow.com/questions/63386834/how-to-get-all-row-information-from-treeview-widget-in-tkinter