The code currently functions correctly by deleting multiple selected rows from the treeview. However, I require the column value associated with the record being deleted from the database. Specifically, I need the student name or any identifier from column A. This value will be utilized in the WHERE clause of the SQL statement (DELETE FROM table WHERE stdid = treeviewcolumnvalue). I would appreciate guidance on how to retrieve the treeview column value from the treeview
this is my code
import tkinter as tk
from tkinter import ttk
from tkinter import *
root = tk.Tk()
root.title("Files for Replication")
tree_frame = tk.Frame(root)
tree_frame.place(relx=0, rely=0, relwidth=0.5, relheight=1)
treeview = ttk.Treeview(tree_frame)
columns = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')
treeview.config(columns=columns, show='headings')
for col in columns:
treeview.column(col)
treeview.heading(col, text=col, anchor=tk.W)
treeview.place(relx=0.02, rely=0.3, relwidth=0.9, relheight=0.5)
def remove_item():
selected_items = treeview.selection()
for selected_item in selected_items:
colvalue = treeview.selection()[0],column='A')
treeview.delete(selected_item)
#treeview.bind('<Shift-Right>', callback_main)
#treeview.bind('<Double-1>', callback_dependencies)
#treeview.insert('', '0', 'item1', text=150*"a")
treeview.insert("", 'end', text ="Ldddd4", values =("Rahul", "M1", "20"))
treeview.insert("", 'end', text ="L6", values =("Rahul", "M2", "20"))
treeview.insert("", 'end', text ="L774", values =("Rahul", "M3", "20"))
treeview.insert("", 'end', text ="L88884", values =("Rahul", "M4", "20"))
treeview.insert("", 'end', text ="L6446664", values =("Rahul", "M5", "20"))
treeview.insert("", 'end', text ="L66664", values =("Rahul", "M6", "20"))
treeview.insert("", 'end', text ="Lddd4", values =("Rahul", "M7", "20"))
treeview.insert("", 'end', text ="Lgggg4", values =("Rahul", "M8", "20"))
tree_scrolly = ttk.Scrollbar(tree_frame, orient=tk.VERTICAL, command=treeview.yview)
tree_scrolly.place(relx=0.91, rely=0.3, relwidth=0.03, relheight=0.49)
treeview.config(yscrollcommand=tree_scrolly.set)
tree_scrollx = ttk.Scrollbar(tree_frame, orient=tk.HORIZONTAL, command=treeview.xview)
tree_scrollx.place(relx=0.02, rely=0.79, relwidth=0.9, relheight=0.03)
treeview.config(xscrollcommand=tree_scrollx.set)
b1 = tk.Button(root, text='delete row----------', width=20,bg='yellow', command=lambda: delete())
b1.pack(side = BOTTOM )
b2 = tk.Button(root, text='delete MULTI rowS =======', width=20,bg='yellow', command=lambda: remove_item())
b2.pack(side = BOTTOM )
b3 = tk.Button(root, text='show =======', width=20,bg='yellow', command=lambda: tree_select)
b3.pack(side = BOTTOM )
root.mainloop()
this line give me error
colvalue = treeview.selection()[0],column='A')
5
To get the value of a specific cell of column ‘A’, you can use treeview.set(selected_item, 'A')
inside remove_item()
:
def remove_item():
selected_items = treeview.selection()
for selected_item in selected_items:
colvalue = treeview.set(selected_item, 'A')
# then use this value to delete record in database table
# if this value is the unique key of the table
# ...
# delete row in treeview
treeview.delete(selected_item)
Note that based on provided data, all values in column ‘A’ is “Rahul”, I think what you need is the values in the tree column instead.