I am trying to update an entry on my tree-view.
The issue I am facing is, when I click to update an entry the ttk.Treeview.focus() methods gives me 'I001' on the first entry for example and with this I cannot update my entry as it gives an error on treeview.insert()
My Code is as below:
`
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Treeview")
root.geometry("650" "x" "450")
root.resizable(True, True)
# Defining Columns:
columns = ("Seller", "Buyer", "Contract Number")
# Calling Treeview:
register = ttk.Treeview(root, columns=columns, show="headings")
register.grid(row=0, column=0, columnspan=3, padx=5, pady=5)
# Inserting headings:
register.heading(columns[0], text="Seller Name")
register.heading(columns[1], text="Buyer Name")
register.heading(columns[2], text="Contract Number")
# Customizing Columns:
register.column(columns[0], anchor=CENTER)
register.column(columns[1], anchor=CENTER)
register.column(columns[2], anchor=CENTER)
# Adding Entries:
def add_entry():
entry = (sn.get(), bn.get(), cn.get())
register.insert("", END, values=entry)
sn.delete(0, END)
bn.delete(0, END)
cn.delete(0, END)
ttk.Label(root, text="Add Entries below:", font=("Arial", 15)).grid(row=1, column=0, pady=5, padx=5, sticky=W)
ttk.Label(root, text="Seller Name:").grid(row=2, column=0, pady=5, padx=5, sticky=W)
ttk.Label(root, text="Buyer Name:").grid(row=3, column=0, pady=5, padx=5, sticky=W)
ttk.Label(root, text="Contract Number:").grid(row=4, column=0, pady=5, padx=5, sticky=W)
sn = ttk.Entry(root)
bn = ttk.Entry(root)
cn = ttk.Entry(root)
sn.grid(row=2, column=1, sticky=W)
bn.grid(row=3, column=1, sticky=W)
cn.grid(row=4, column=1, sticky=W)
add = ttk.Button(root, text="Add Entry", command=add_entry)
add.grid(row=5, column=0, columnspan=3, padx=5, pady=5, sticky=W)
# Variables:
update = IntVar()
# Deleting Entries / Updating Entries:
def item_selected(event):
if update.get() == 0:
for selection in register.selection():
register.delete(selection)
else:
sn.delete(0, END), bn.delete(0, END), cn.delete(0, END)
for selection in register.selection():
x = register.item(selection)["values"]
sn.insert(0, x[0]), bn.insert(0, x[1]), cn.insert(0, x[2])
register.bind('<<TreeviewSelect>>', item_selected)
# Updating Entries:
def update_record():
x = register.focus()[-1]
new_value = (sn.get(), bn.get(), cn.get())
for selection in register.selection():
register.delete(selection)
register.insert("", int(x), values=new_value)
cb = ttk.Checkbutton(root, text="Update Records (on click)", variable=update)
cb.grid(row=6, column=0, padx=5, pady=5, sticky=W)
up = ttk.Button(root, text="Update Record", command=update_record)
up.grid(row=6, column=1, padx=15, pady=10, sticky=W)
root.mainloop()
`
x = register.focus()[-1], due to this work around I can sort of update my entries, but this isn't a permeant solution or efficient way of doing this.
So please help me with this.
CodePudding user response:
You can simply update the selected row instead of deleting row and then inserting row:
def update_record():
# get item ID (iid) of selected row
x = register.focus()
if x:
# a row is selected
new_value = (sn.get(), bn.get(), cn.get())
# update the row using the item ID of selected row
register.item(x, values=new_value)