0

a programming beginner here, I was tasked to do a homework regarding UI in python. The task was a sort of a create, update and delete system(No databases included) but it has to have OOP. So I decided to use the ttk.Treeview in python. There was not much trouble in getting create and delete working, but when I tried to to do the update function I was lost and none that I have tried worked so far.

Regarding the ones I have tried, I tried to apply the answer to How to make ttk.Treeview's rows editable?. But I could not understand it because I am still just a beginner and every other ones I have seen, they all use databases, which is not allowed in this homework.

As for my code it's fairly short:

from tkinter import messagebox, ttk

employees = []

class Employees:

    def __init__(self, n, d, p, r):
        self.n = n
        self.d = d
        self.p = p
        self.r = r


def add():
    n = e1.get()
    d = e2.get()
    p = e3.get()
    r = e4.get()

    employees.append(Employees(n, d, p, r))

    tview.insert('', "end", text=n, values=(d, p, r))
    messagebox.showinfo("Add", "Successfully added")

def delete():
    selected_item = tview.selection()[0]
    tview.delete(selected_item)

def updatetreeview():
    # here where I am lost at I don't know what to do
    selected_item = tview.selection()[0]


master = Tk()

Label(master, text='Name').grid(row=0)
Label(master, text='Department').grid(row=1)
Label(master, text='Position').grid(row=2)
Label(master, text='Rate').grid(row=3)
tview = ttk.Treeview(master, columns=('Name', 'Position', 'Department','Rate'))
tview.grid(row=7, column=0, columnspan=10)

tview.heading('#0', text="Name")
tview.heading('#1', text="Department")
tview.heading('#2', text="Position")
tview.heading('#3', text="Rate")


e1 = Entry(master, width="30")
e2 = Entry(master, width="30")
e3 = Entry(master, width="30")
e4 = Entry(master, width="30")

e1.grid(row=0, column=1, pady=10)
e2.grid(row=1, column=1, pady=10)
e3.grid(row=2, column=1, pady=10)
e4.grid(row=3, column=1, pady=10)

b1 = Button(master, text="Add", width="25", command=add)
b1.grid(row=4, column=1, pady=10)
b2 = Button(master, text="Update", width="25")
b2.grid(row=5, column=1, pady=10)
b2 = Button(master, text="Delete", width="25", command=delete)
b2.grid(row=6, column=1, pady=10)
mainloop()

As for my expected results, I expect for the selected row in the table to be updated upon a button press.

martineau
  • 119,623
  • 25
  • 170
  • 301
Holden
  • 17
  • 6
  • I don't see how this question is any different than the one you linked to. Why should we keep this open instead of closing it as a duplicate? – Bryan Oakley Sep 11 '19 at 04:08

2 Answers2

0

I believe the word you are looking for should be "updating" a Treeview item instead of editable. From the docs:

item(item, option=None, **kw)

Query or modify the options for the specified item.

If no options are given, a dict with options/values for the item is returned. If option is specified then the value for that option is returned. Otherwise, sets the options to the corresponding values as given by kw.

So for your case, you have retrieved the iid already by tview.selection()[0]. Use the item method to modify the record:

def updatetreeview():
    selected_item = tview.selection()[0]
    tview.item(selected_item,text=e1.get(), values=(e2.get(),e3.get(),e4.get()))

...

b2 = Button(master, text="Update", width="25",command=updatetreeview)

...
Henry Yik
  • 22,275
  • 4
  • 18
  • 40
  • Wow, thank you so much!! It works like a charm, I was really having trouble there, you are a godsend, thank you for helping this noob out – Holden Sep 11 '19 at 13:35
0

Your function to update the content in the row is incomplete, you are retrieving the content using the treeview.selection() method after that you have to use treeview item to update the content in the treeview.

Have added exception handling to the update function to prompt you to select the error you want to update if not it will output an error.

Also have to clear the entry after you add or update the treeview.

from tkinter import messagebox, ttk
from tkinter import *

employees = []

class Employees:

    def __init__(self, n, d, p, r):
        self.n = n
        self.d = d
        self.p = p
        self.r = r


def add():
    n = e1.get()
    d = e2.get()
    p = e3.get()
    r = e4.get()


    employees.append(Employees(n, d, p, r))

    tview.insert('', "end", text=n, values=(d, p, r))
    messagebox.showinfo("Add", "Successfully added")

    e1.delete(0, END)
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)


def delete():
    try:
        selected_item = tview.selection()[0]
        tview.delete(selected_item)
    except IndexError:
        pass


def updatetreeview():
    try:
        selected_item = tview.selection()[0]
        tview.item(selected_item, text=e1.get(), values=(e2.get(), e3.get(), e4.get()))
        print("updated")

        e1.delete(0, END)
        e2.delete(0, END)
        e3.delete(0, END)
        e4.delete(0, END)

    except IndexError:
        messagebox.showerror("Error","Select the row you want to update")



master = Tk()

Label(master, text='Name').grid(row=0)
Label(master, text='Department').grid(row=1)
Label(master, text='Position').grid(row=2)
Label(master, text='Rate').grid(row=3)
tview = ttk.Treeview(master, columns=('Name', 'Position', 'Department','Rate'))
tview.grid(row=7, column=0, columnspan=10)

tview.heading('#0', text="Name")
tview.heading('#1', text="Department")
tview.heading('#2', text="Position")
tview.heading('#3', text="Rate")


e1 = Entry(master, width="30")
e2 = Entry(master, width="30")
e3 = Entry(master, width="30")
e4 = Entry(master, width="30")

e1.grid(row=0, column=1, pady=10)
e2.grid(row=1, column=1, pady=10)
e3.grid(row=2, column=1, pady=10)
e4.grid(row=3, column=1, pady=10)

b1 = Button(master, text="Add", width="25", command=add)
b1.grid(row=4, column=1, pady=10)
b2 = Button(master, text="Update", width="25", command=updatetreeview)
b2.grid(row=5, column=1, pady=10)
b2 = Button(master, text="Delete", width="25", command=delete)
b2.grid(row=6, column=1, pady=10)

mainloop()
AD WAN
  • 1,414
  • 2
  • 15
  • 28